node.js module.exports & exports & module.export all in one

node.js module.exports & exports & module.export all in one

cjs

const log = console.log;

log(`exports`, exports);
log(`module`, module); // TypeError: Cannot set property 'a' of undefined
// module.export.a = 1;
// module.export.b = 2;
// module.export.c = 3;
// module.export.d = 4; // 自定义属性 umd
module.umd = {
a: 1,
b: 2,
c: 3,
d: 4,
};
// 自定义属性 export
module.export = {
a: 1,
b: 2,
c: 3,
d: 4,
}; exports.a = 11;
exports.b = 22;
exports.c = 33;
exports.d = 44; // module.exports 覆盖 exports
module.exports = { c: 333 };
module.exports.d = 444; log(`\nexports`, exports);
log(`module`, module); /* exports { a: 11, b: 22, c: 33, d: 44 }
module Module {
id: '.',
path: '/Users/xgqfrms-mbp/Documents/GitHub/umd-npm-package/src',
exports: { c: 333, d: 444 },
parent: null,
filename: '/Users/xgqfrms-mbp/Documents/GitHub/umd-npm-package/src/export.js',
loaded: false,
children: [],
paths: [
'/Users/xgqfrms-mbp/Documents/GitHub/umd-npm-package/src/node_modules',
'/Users/xgqfrms-mbp/Documents/GitHub/umd-npm-package/node_modules',
'/Users/xgqfrms-mbp/Documents/GitHub/node_modules',
'/Users/xgqfrms-mbp/Documents/node_modules',
'/Users/xgqfrms-mbp/node_modules',
'/Users/node_modules',
'/node_modules'
],
umd: { a: 1, b: 2, c: 3, d: 4 },
export: { a: 1, b: 2, c: 3, d: 4 }
}
*/

module.exports vs exports

const log = console.log;

const app = (datas = [], debug = false) => {
log(`args =`, datas);
}; const test = `abc`; // default export (const app = require(`./app`);)
module.exports = app; module.exports.test = test; //
// module.exports.app = app; //
// module.exports = {
// app,
// }; log(`exports =`, exports);
log(`module.exports =`, module.exports);
// exports = {}
// module.exports = [Function: app] { test: 'abc' }
// log(`module`, module);
const log = console.log;

//  import default module
const app = require(`./app`);
// const app, {test} = require(`./app`); log(`\napp =`, app, app.app);
app();
// app = [Function: app] undefined
// args = [] log(`test =`, app.test);
// test = abc
const log = console.log;

const app = (datas = [], debug = false) => {
log(`args =`, datas);
}; const test = `abc`; //
// exports = app; // module export (const { app, } = require(`./app`);)
exports.app = app; exports.test = test; log(`exports =`, exports);
log(`module.exports =`, module.exports);
// exports = { app: [Function: app], test: 'abc' }
// module.exports = { app: [Function: app], test: 'abc' }
// log(`module`, module);
const log = console.log;

//  imports multi modules
const { app, test, } = require(`./app`); log(`\napp =`, app);
app();
// app = [Function: app]
// args = [] log(`test =`, test);
// test = abc

node.js module.exports & exports & module.export all in one

exports = module.exports

module.exports 与 exports 指向同一个Object 引用

https://blog.tableflip.io/the-difference-between-module-exports-and-exports

node.js module.exports & exports & module.export all in one

"use strict";

/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-10-01
* @modified
*
* @description
* @difficulty Easy Medium Hard
* @complexity O(n)
* @augments
* @example
* @link
* @solutions
*
* @best_solutions
*
*/ const log = console.log; // module.exports 与 exports 指向同一个Object 引用 // SyntaxError: Identifier 'module' has already been declared
// module = {
// exports: {},
// //others
// };
// // {exports: {…}} // module.exports;
// // {} // exports = module.exports;
// // {} // exports.a = `a`; // module.exports.a = `aa`;
// module.exports.b = `b`; // log(`exports =`, exports);
// log(`module.exports =`, module.exports); /* exports = { a: 'aa', b: 'b' }
module.exports = { a: 'aa', b: 'b' } */ const test = () => {
const module = {
exports: {},
//others
};
// {exports: {…}}
module.exports;
// {}
const exports = module.exports;
// {}
exports.a = `a`;
module.exports.a = `aa`;
module.exports.b = `b`;
log(`exports =`, exports);
log(`module.exports =`, module.exports);
}
test(); /* exports = { a: 'aa', b: 'b' }
module.exports = { a: 'aa', b: 'b' } */

refs

https://gist.github.com/xgqfrms/b69677e524d4c4e154fef6342914eb00


node.js module.exports & exports & module.export all in one

xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


上一篇:2019年1月16日22:50:28 白糖SR1905


下一篇:Linux系统批量化安装部署之Cobbler