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. C#9.0:Records

    概述 在C#9.0下,record是一个关键字,微软官方目前暂时将它翻译为记录类型. 传统面向对象的编程的核心思想是一个对象有着唯一标识,封装着随时可变的状态.C#也是一直这样设计和工作的.但是一些时 ...

  2. Mysql,Oracle与Java字段类型映射关系

    Mysql,Oracle与Java字段类型映射关系 参考相关博文 MySQL/Oracle字段类型 Java字段类型 最大长度 BIT java.lang.Boolean 1 BLOB java.la ...

  3. java--Aop--记录日志

    package com.pt.modules.log; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; imp ...

  4. 31.FTP简介

    1.FTP 是一种在互联网中进行文件传输的协议,基于客户端/服务器模式,默认使用20.21号端口,其中端口20(数据端口)用于进行数据传输,端口21(命令端口)用于接受客户端发出的相关FTP 命令与参 ...

  5. 2.centos 7清空文件和文件夹

    1.清空文件 测试文件:a.txt 1)方法一,[root@centos test]# > a.txt [root@centos test]# cat a.txt 1hjbfao hjkl23o ...

  6. 云服务器镜像问题("Couldn't resolve host 'mirrors.tencentyun.com')

    云服务器镜像问题("Couldn't resolve host 'mirrors.tencentyun.com') 原因: 腾讯云服务器内网yum源的域名 mirrors.tencentyu ...

  7. A - 欧拉回路

    欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路.现给定一个图,问是否存在欧拉回路? Input测试输入包含若干测试用例.每个测试用例的第1行给出两个正整数,分别是节点数N ...

  8. CF-1291 D - Irreducible Anagrams

    D. Irreducible Anagrams 题意 若两个字符串中每个字符的个数都是一样的,则称他们互为\(anagrams\).现在定义两个字符串s,t是\(reducible~anagram\) ...

  9. Codeforces Round #582 (Div. 3) F. Unstable String Sort

    传送门 题意: 你需要输出一个长度为n的字符序列(由小写字母组成),且这个字符串中至少包含k个不同的字符.另外题目还有要求:给你两个长度为p和q的序列,设字符序列存在s中 那么就会有s[Pi]< ...

  10. Codeforces #Round 632 div2 A~C

                                       A. Little Artem   Young boy Artem tries to paint a picture, and h ...