Node.js & module.exports & exports

https://www.cnblogs.com/xgqfrms/p/9493550.html

exports & module.exports

  1. Functions and objects are added to the root of a module by specifying additional properties on the special exports object.

module.exports & exports

CJS Modules


// CJS Modules /*
exports.a = 1;
exports.b = 2;
exports.c = 3;
exports.d = 4; // exports = { a: 1, b: 2, c: 3, d: 4 } */ exports.a = 1;
exports.b = 2;
module.exports = { c: 3 };
module.exports.d = 4; // module.exports = { c: 3, d: 4 }

结论

  1. exports just shorthand of module.exports

  2. if exist both exports & module.exports, module.exports will be overwritten exports

  3. best practice, just using module.exports as possible as you can


exports.a = 1;
exports.b = 2;
module.exports = { c: 3 };
module.exports.d = 4; // module.exports = { c: 3, d: 4 } console.log(module);


exports.a = 1;
exports.b = 2;
module.exports = { c: 3 };
module.exports.d = 4; // module.exports = { c: 3, d: 4 } module.exports.hello = true; // Exported from require of module
exports = { hello: false }; // Not exported, only available in the module console.log(`hello =`, exports);
console.log(module);

https://www.sitepoint.com/understanding-module-exports-exports-node-js#whatsthedifferencebetweenmoduleexportsandexports

https://www.hacksparrow.com/nodejs/exports-vs-module-exports.html

freecodecamp

https://www.freecodecamp.org/news/node-js-module-exports-vs-exports-ec7e254d63ac/

refs

https://www.w3schools.com/nodejs/nodejs_modules.asp



xgqfrms 2012-2020

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


Node.js & module.exports & exports的更多相关文章

  1. (译)Node.js的模块-exports和module.exports

    原文标题:Node.js Module – exports vs module.exports 原文链接:http://www.hacksparrow.com/node-js-exports-vs-m ...

  2. (转)Node.js module.exports与exports

    本文转自Node.js module.exports与exports 作者: chemdemo 折腾Node.js有些日子了,下面将陆陆续续记录下使用Node.js的一些细节. 熟悉Node.js的童 ...

  3. 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 ...

  4. 创建并发布node.js module

      创建node.js module. 创建一个文件夹,用来存放module. Cd到新创建的文件夹,运行npm init,会提示输入package的信息. 可以按照这个视频的来输入.Test com ...

  5. Node.js & module system

    Node.js & module system Node.js v10.9.0 Documentation https://nodejs.org/api/modules.html#module ...

  6. 如何發佈一個完整Node.js Module

    本文會透過以下幾個段落,讓各位一步一步學習如何寫一個自已的Node.js Module並且發佈到npm package上 Node.js Module 結構 我們先建立一個 NodeModuleDem ...

  7. Node.js module.exports和exports的区别

    require 用来加载代码,而 exports 和 module.exports 则用来导出代码,从接触node.js就不会它们两陌生,上代码: foo.js exports.a = functio ...

  8. Node.js模块导出exports 和 module.exports 的区别

    原文: https://blog.csdn.net/Pwiling/article/details/51958693 每一个node.js执行文件,都自动创建一个module对象,同时,module对 ...

  9. node.js模块中exports和module.exports的区别

    Node应用由模块组成,采用CommonJS模块规范. 根据这个规范,每个文件就是一个模块,有自己的作用域.在一个文件里面定义的变量.函数.类,都是私有的,对其他文件不可见. CommonJS规范规定 ...

随机推荐

  1. css知识补充

    盒子模型的介绍: 在网页中基本上都会显示一些方方正正的盒子,这种盒子就被我们称为盒模型 盒子模型的五个属性: width,height,border(边框),padding(内边距),margin(外 ...

  2. 在ubuntu编写helloworld

    安装vim 打开终端 输入sudo apt-get install vim-gtk 输入登陆密码 等待安装完成 编译C 创建.c文件:vim helloworld.c 编写代码,保存并退出 编译:gc ...

  3. how2j SpringMVC学习心得

    http://how2j.cn/k/springmvc/springmvc-form/618.html 注意 addProduct.jsp 是放在了WebContent(即web目录)下,访问的时候, ...

  4. Excel 如何使用 函数 实现传说中的 多条件中位数 (MEDIANIFS)?

    数据科学交流群,群号:189158789,欢迎各位对数据科学感兴趣的小伙伴的加入! 实际上Excel中根本没有MEDIANIFS,但我们可以通过一些方式进行实现: 比如如下图的需求,需要求这一列中,1 ...

  5. React-Router browserHistory浏览器刷新出现页面404解决方案

    在React项目中我们经常需要采用React-Router来配置我们的页面路由,React-Router 是建立在 history 之上的,常见的history路由方案有三种形式,分别是: 1.has ...

  6. Java实现发送HTTP的POST请求,返回数据以及请求状态

    /** * @param url:请求url * @param content: 请求体(参数) * @return errorStr:错误信息;status:状态码,response:返回数据 */ ...

  7. centos6.5 更新python2.6.6至2.7.6

    1.查看当前Python版本    # python -V      Python 2.6.6  2.下载Python2.7.6源码    # wget http://python.org/ftp/p ...

  8. linux 一分钟搭建zookeeper linux 单机版(亲测可用)

    wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gzt ...

  9. ElasticSearch 安装与运行

    公号:码农充电站pro 主页:https://codeshellme.github.io 本节来介绍 ES 的安装. 1,下载 ES ES 是基于 Java 语言开发的,因此,要安装 ES,首先需要有 ...

  10. [WPF 学习] 18. 摄像头(肢解DirectShow)

    公司的产品需要人脸比对,摄像头相关的需求如下(突然发现除了英文不太好外,实际上中文也不太好,所以直接上一个接口) using System; using System.Drawing; using S ...