介绍

  • module:每个模块中都有 module 对象,存放了当前模块相关的信息;
  • module.exports:模块导出的内容;
  • exports:默认情况下,exports 和 module.exports 指向同一个对象。

示例

test.js

console.log('module', module)
console.log('module.exports', module.exports)
console.log('exports', exports)
console.log('module.exports === exports', module.exports === exports)

控制台执行 node test.js打印日志如下:

module Module {
  id: '.',
  path: 'E:\lin\webpack-learning\src\cjs\demo1',
  exports: {},
  filename: 'E:\lin\webpack-learning\src\cjs\demo1\test.js',
  loaded: false,
  children: [],
  paths: [
    'E:\lin\webpack-learning\src\cjs\demo1\node_modules',
    'E:\lin\webpack-learning\src\cjs\node_modules',
    'E:\lin\webpack-learning\src\node_modules',
    'E:\lin\webpack-learning\node_modules',
    'E:\lin\node_modules',
    'E:\node_modules'
  ]
}
module.exports {}
exports {}
module.exports === exports true

从源码中理解

github.com/nodejs/node…

const exports = this.exports;
const thisValue = exports;
const module = this;

说明:exports 是 module.exports 的引用

通过示例理解

示例一

test1.js

exports.name = 'lin';
module.exports.age = 18;

console.log('module.exports', module.exports)
console.log('exports', exports)
console.log('module.exports === exports', module.exports === exports)

index.js

const test = require('./test1')
console.log("test", test);

控制台执行 node index.js,打印日志如下:

module.exports { name: 'lin', age: 18 }
exports { name: 'lin', age: 18 }
module.exports === exports true 
test { name: 'lin', age: 18 }

画图说明:

示例二

test2.js

module.exports.name = 'lin'
exports = {
  name: 'myName',
  age: 6
}
console.log('module.exports', module.exports)
console.log('exports', exports)
console.log('module.exports === exports', module.exports === exports)

index.js 改为引入 test2.js 模块

const test = require('./test2')
console.log("test", test);

控制台执行 node index.js打印日志如下:

module.exports { name: 'lin' }
exports { name: 'myName', age: 6 }
module.exports === exports false
test { name: 'lin' }

画图说明:

示例三

test3.js

module.exports = {
  name: 'lin',
  age: 18
}

exports.name = "myName"
console.log('module.exports', module.exports)
console.log('exports', exports)
console.log('module.exports === exports', module.exports === exports)

index.js 改为引入 test3.js 模块

const test = require('./test3')
console.log("test", test);

控制台执行 node index.js打印日志如下:

module.exports { name: 'lin', age: 18 }
exports { name: 'myName' }
module.exports === exports false
test { name: 'lin', age: 18 }  

画图说明:

示例四

test4.js

exports = {
  name: 'lin',
  age: 18
}
module.exports = exports
module.exports.job = 'FE'
console.log('module.exports', module.exports)
console.log('exports', exports)
console.log('module.exports === exports', module.exports === exports)

index.js 改为引入 test4.js 模块

const test = require('./test4')
console.log("test", test);

控制台执行 node index.js打印日志如下:

module.exports { name: 'lin', age: 18, job: 'FE' }
exports { name: 'lin', age: 18, job: 'FE' }
module.exports === exports true
test { name: 'lin', age: 18, job: 'FE' }

画图说明:

小结

  • exports 是 module.exports 的引用;
  • 对 exports 和 module.exports 赋值时要格外注意,明确模块导出的值;
  • 使用 require() 导入模块 A 时,导入的结果是模块 A 中 module.exports 指向的值。

到此这篇关于Node.js 中的 module.exports 与 exports区别介绍的文章就介绍到这了,更多相关Node.js exports内容请搜索Devmax以前的文章或继续浏览下面的相关文章希望大家以后多多支持Devmax!

Node.js 中的 module.exports 与 exports区别介绍的更多相关文章

  1. CentOS 8.2服务器上安装最新版Node.js的方法

    这篇文章主要介绍了CentOS 8.2服务器上安装最新版Node.js的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  2. node.js三个步骤实现一个服务器及Express包使用

    这篇文章主要介绍了node.js三个步骤实现一个服务器及Express包使用,文章通过新建一个文件展开全文内容,具有一定的参考价值,需要的小伙伴可以参考一下

  3. Node.js调试技术总结分享

    Node.js是一个可以快速构建网络服务及应用的平台。该平台的构建是基于Chrome's JavaScript runtime,也就是说,实际上它是对Google V8引擎(应用于Google Chrome浏览器)进行了封装。 今天介绍Node.js调式目前有几种技术,需要的朋友可以参考下。

  4. node.js实现http服务器与浏览器之间的内容缓存操作示例

    这篇文章主要介绍了node.js实现http服务器与浏览器之间的内容缓存操作,结合实例形式分析了node.js http服务器与浏览器之间的内容缓存原理与具体实现技巧,需要的朋友可以参考下

  5. 教你如何使用node.js制作代理服务器

    本文介绍了如何使用node.js制作代理服务器,图文并茂,十分的详细,代码很简洁易懂,这里推荐给大家。

  6. node.js中的fs.openSync方法使用说明

    这篇文章主要介绍了node.js中的fs.openSync方法使用说明,本文介绍了fs.openSync方法说明、语法、接收参数、使用实例和实现源码,需要的朋友可以参考下

  7. Node.js+ELK日志规范的实现

    这篇文章主要介绍了Node.js+ELK日志规范的实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  8. node.js爬虫框架node-crawler初体验

    这篇文章主要介绍了node.js爬虫框架node-crawler的相关资料,帮助大家利用node.js进行爬虫,感兴趣的朋友可以了解下

  9. node.js中的fs.existsSync方法使用说明

    这篇文章主要介绍了node.js中的fs.existsSync方法使用说明,本文介绍了fs.existsSync方法说明、语法、接收参数、使用实例和实现源码,需要的朋友可以参考下

  10. 说说如何利用 Node.js 代理解决跨域问题

    这篇文章主要介绍了Node.js代理解决跨域问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

随机推荐

  1. Error: Cannot find module ‘node:util‘问题解决

    控制台 安装 Vue-Cli 最后一步出现 Error: Cannot find module 'node:util' 问题解决方案1.问题C:\Windows\System32>cnpm install -g @vue/cli@4.0.3internal/modules/cjs/loader.js:638 throw err; &nbs

  2. yarn的安装和使用(全网最详细)

    一、yarn的简介:Yarn是facebook发布的一款取代npm的包管理工具。二、yarn的特点:速度超快。Yarn 缓存了每个下载过的包,所以再次使用时无需重复下载。 同时利用并行下载以最大化资源利用率,因此安装速度更快。超级安全。在执行代码之前,Yarn 会通过算法校验每个安装包的完整性。超级可靠。使用详细、简洁的锁文件格式和明确的安装算法,Yarn 能够保证在不同系统上无差异的工作。三、y

  3. 前端环境 本机可切换node多版本 问题源头是node使用的高版本

    前言投降投降 重头再来 重装环境 也就分分钟的事 偏要折腾 这下好了1天了 还没折腾出来问题的源头是node 使用的高版本 方案那就用 本机可切换多版本最终问题是因为nodejs的版本太高,导致的node-sass不兼容问题,我的node是v16.14.0的版本,项目中用了"node-sass": "^4.7.2"版本,无法匹配当前的node版本根据文章的提

  4. nodejs模块学习之connect解析

    这篇文章主要介绍了nodejs模块学习之connect解析,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  5. nodejs npm package.json中文文档

    这篇文章主要介绍了nodejs npm package.json中文文档,本文档中描述的很多行为都受npm-config(7)的影响,需要的朋友可以参考下

  6. 详解koa2学习中使用 async 、await、promise解决异步的问题

    这篇文章主要介绍了详解koa2学习中使用 async 、await、promise解决异步的问题,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  7. Node.js编写爬虫的基本思路及抓取百度图片的实例分享

    这篇文章主要介绍了Node.js编写爬虫的基本思路及抓取百度图片的实例分享,其中作者提到了需要特别注意GBK转码的转码问题,需要的朋友可以参考下

  8. CentOS 8.2服务器上安装最新版Node.js的方法

    这篇文章主要介绍了CentOS 8.2服务器上安装最新版Node.js的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  9. node.js三个步骤实现一个服务器及Express包使用

    这篇文章主要介绍了node.js三个步骤实现一个服务器及Express包使用,文章通过新建一个文件展开全文内容,具有一定的参考价值,需要的小伙伴可以参考一下

  10. node下使用UglifyJS压缩合并JS文件的方法

    下面小编就为大家分享一篇node下使用UglifyJS压缩合并JS文件的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

返回
顶部