刚开始接触nodejs的同学对module.exports和exports的使用会有困惑,到底该使用哪一个,还是两个都可以使用,要理解二者的区别,应该先理解nodejs中require的加载机制。
// 准备module对象: var module = { id: 'hello', exports: {} }; var load = function (module,exports) { // 下面是加载的js代码: let foo='hello'; module.exports = foo; // 最后返回module.exports return module.exports; }; var exported = load(module,module.exports); // 保存module: save(module, exported);
从中可以看出一个模块被引用的时候最终都会被输出成module.exports,而exports只是module.exports的一个引用,说到这里大家应该只能二者区别了。
下面例子
let name = "张三"; let age = 18; let sayhello = () => { console.log(name); } exports.name=name; exports.age=age; exports.sayhello=sayhello;
等价于
let name = "张三"; let age = 18; let sayhello = () => { console.log(name); } module.exports.name=name; module.exports.age=age; module.exports.sayhello=sayhello;
等价于
let name = "张三"; let age = 18; let sayhello = () => { console.log(name); } module.exports={name,age,sayhello};
但是不能这样写,如果对exports对象重新赋值,那么最终module.exports的值将是空对象。
let name = "张三"; let age = 18; let sayhello = () => { console.log(name); } exports={name,age,sayhello};
总结:只要不是直接导出对象,使用module.exports和exports没有区别,要是导出的对象只能使用module.exports
相关推荐
plugin为Babel和TypeScript编译的代码添加module.exports当您将ES2015模块与Babel一起使用或在TypeScript中具有默认导出时,它们会生成代码,要求您使用CommonJS中的require('x')。default而不是require来导入它。...
因为SeaJs和Nodejs都是基于CommonJS,所以直接看的Node的官方文档解释 Module.exports The module.exports object is created by the Module system. Sometimes this is not acceptable; many want their module to ...
UnCommonJS要求要求出口默认洞穴范围发展兼容性也可以看看版本作者版权和许可 名称UnCommonJS-module.exports的最小可行填充module.exports 特征module.exports exports 可插拔的require 支持实时导出(ESM仿真) ...
nodejs 采用的commonjs模块规范 一个js文件就是一个模块 重点 : 导出 module.exports exports.变量 = 导出的对象 不能让exports直接指向一个对象, 因为,exports 就 不再指向module.exports了, 指向新对象了 ...
因为是做前端的,对node的生态一直也比较关注,对于node中对commonJS模块化的实现给了我们很大的方便,之前对于导出的module.exports和exports一直模模糊糊,今天做一个整理 先来个js基础部分的复习 let obj1 = {} let...
nodejs 采用的commonjs模块规范 一个js文件就是一个模块 重点 : 导出 module.exports exports.变量 = 导出的对象 不能让exports直接指向一个对象, 因为,exports 就 不再指向module.exports了, 指向新对象了 ...
commonjs-to-es-module-codemod jscodeshift codemod将CommonJS(require / exports)转换为ES Modules(import / export)用于JavaScript / TypeScript 支持 出口产品 命名为export: module.exports.foo = foo以...
社区模块规范: 1.CommonJS规范 规范实现者: NodeJS 服务端 Browserify 浏览器 2.AMD规范 全称 异步模块定义 ...1.module.exports实质上是一个对象,最后模块导出的对象就是这个引用指向的对象 module
ES6和CommonJS都有自己的一套处理模块化代码的措施,即JS文件之间的相互引用。 为了方便两种方式的测试,使用nodejs的环境进行测试 CommonJS的模块处理 使用require来引入其他模块的代码,使用module.exports来引出 ...
### module.exports最初,你的模块将有一个对象两个名字: module.exports和exports 。 按照惯例,出于实际原因,导出是通过以下两种方式之一完成的: 为exports分配属性: exports.property = value; 覆盖module...
webpack custom babelrc .babelrc配置 主要有 对 预设presets 和插件 plugins 进行配置 presets 创建预设 早起有很多插件 check-es2015-...modules 默认false commonjs规范 就是 module.exports targets 内 bor
common 中 export module.exports import export default的用法
它允许您使用 Commonjs/NodeJS 风格的“require()”来处理客户端项目中的依赖项,而无需编译任何东西。 用法非常简单:只需将其包含在您的 HTML 文件中,并将其指向您的 Entry-JS 文件,如下所示: [removed]...
CommonJS vs模块比较该存储库显示CommonJS( require , module.exports )和Module( import , export )节点实现之间的区别。 普通JS 模块观察与恢复即使代码是相同的,唯一的不同是导入,代码执行的顺序也大不...
是的 全局安装 npm i -g ish选择一些文件,运行它们,并将它们的module.exports属性注入到新 REPL 的全局范围中。 根据调用者是bin/ish还是其他脚本,在正确解析文件路径方面可能存在一些挥之不去的边缘情况。 给定...
module.exports = factory(); } else if (typeof define === 'function' && define.amd) { // AMD define(factory); } else { // Browser globals (with support for web workers) var glob; try { glob =...
1. CommonJS 用于服务端模块化编程,比如nodejs就采用此规范; 一个文件就是一个模块,require方法用来加载模块,该方法读取一个文件并执行,最后返回文件内部的module.exports对象; require是默认读取.js文件的,...
function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Babel=t():e.Babel=t()}(this,...
require和import的区别require和import的区别导入require 导出 exports/module.exports 是 CommonJS
语法扩展在 Nodejs(CommonJS)中导出模块,只需要将导出对象赋值给 module.exports 即可,而 TypeScript 的模块系统采用的是