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

exports = module.exports
module.exports 与 exports 指向同一个Object 引用
https://blog.tableflip.io/the-difference-between-module-exports-and-exports

"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
xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
node.js module.exports & exports & module.export all in one的更多相关文章
- (译)Node.js的模块-exports和module.exports
原文标题:Node.js Module – exports vs module.exports 原文链接:http://www.hacksparrow.com/node-js-exports-vs-m ...
- Node.js模块导出exports 和 module.exports 的区别
原文: https://blog.csdn.net/Pwiling/article/details/51958693 每一个node.js执行文件,都自动创建一个module对象,同时,module对 ...
- node.js中的exports和module.exports
不同的编程语言都有各自的代码组织和复用的方式,如.net.php中的命名空间,python中的import,ruby中的module等,来避免命名空间污染.一直都没搞清楚node中的exports和m ...
- node.js模块中exports和module.exports的区别
Node应用由模块组成,采用CommonJS模块规范. 根据这个规范,每个文件就是一个模块,有自己的作用域.在一个文件里面定义的变量.函数.类,都是私有的,对其他文件不可见. CommonJS规范规定 ...
- Node.js中的exports与module.exports的区分
1. module应该是require方法中,上下文中的对象 2. exports对象应该是上下文中引用module.exports的新对象 3. exports.a = xxx 会将修改更新到mod ...
- Node.js Error: Cannot find module express的解决办法
1.全局安装express框架,cmd打开命令行,输入如下命令: npm install -g express express 4.x版本中将命令工具分出来,安装一个命令工具,执行命令: npm in ...
- Node.js Error: Cannot find module express的解决办法(转载)
1.全局安装express框架,cmd打开命令行,输入如下命令: npm install -g express express 4.x版本中将命令工具分出来,安装一个命令工具,执行命令: npm in ...
- node启动服务报错Node.js Error: Cannot find module express
在node文件夹中(M:\express-test),执行 npm install express 在使用npm安装express时,报npm WARN saveError ENOENT: no su ...
- es6 import export 与 node 中的module.exports exports
1.export a.export 变量 export var name = 'jack';export var age = 18;//等同于 var name = 'jack';var age = ...
- Node.js中exports,module.exports以及require方法
在Node.js中,使用module.exports.f = ...与使用exports.f = ...是一样的,此时exports就是module.exports的一种简写方式.但是,需要注意的是, ...
随机推荐
- cisco思科交换机终端远程ssh另一端报错:% ssh connections not permitted from this terminal
故障现象: XSJ-GH10-C3750->ssh 58.64.xx.xx% ssh connections not permitted from this terminal 解决办法: 原因: ...
- Python+Selenium+Unittest实现PO模式web自动化框架(2)
1.Common目录下的具体模块讲解. 2.basepage.py basepage.py模块里面是封装的对元素的操作.例如:查找元素.点击元素.文本输入等等. # --^_^-- coding:ut ...
- (14)-Python3之--虚拟环境virtualenv
1.安装virtualenv pip install virtualenv 如果是在Linux下需要把virtualenv添加到/usr/bin目录下 # find / -name virtualen ...
- Mysql四种通信协议(linux下本地连接的都是socket 其他都是tcp)
Mysql通信协议 - 张冲andy - 博客园 https://www.cnblogs.com/andy6/p/6204579.html
- 获取当前文件路径 import 原理 一般把模块组成的集合称为包(package)
获取当前文件路径 testpath.py import sysprint(sys.path) [root@d mapReduceLog]# python testpath.py['/data/mapR ...
- 命名规范 api-guidelines api规范
https://weui.io weui.css .weui-cell_select-before .weui-cell__bd:after{ display:none; } .weui-cell_s ...
- TCMalloc源码学习(四)(小内存块释放)
pagemap_和pagemap_cache_ PageHeap有两个map,pagemap_记录某一内存页对应哪一个span,显然可能多页对应一个span,pagemap_cache_记录某一内存页 ...
- 一文带你看遍 JDK9~14 的重要新特性!
Java9 发布于 2017 年 9 月 21 日 .作为 Java8 之后 3 年半才发布的新版本,Java 9 带 来了很多重大的变化其中最重要的改动是 Java 平台模块系统的引入,其他还有诸如 ...
- 在Centos7上安装Python+Selenium+Chrome+Chromedriver
1.下载Chrome 上一篇文章已经演示过了Python+Selenium+Firefox+Geckodriver安装步骤并通过自动化脚本打开百度 因此当前只需要安装Chrome和Chromedriv ...
- Vue技术点整理-vue.config.js
1,proxy代理解决本地开发环境跨域问题 配置proxy代理后,proxy会将任何未知请求 (没有匹配到静态文件的请求) 代理到 https://192.168.3.49:8080 vue.conf ...