require 用于引入模块(js文件)、 JSON、或本地文件 自己写的模块必须是相对路径,省略了node就认为该自定义模块(js文件)是核心模块(内置模块或者第三方模块)
node  有模块作用域,也就是说不同js文件里面即使变量同名,也互不影响。
 

模块之间通信

exports 和module.exports 默认是空对象{}.把需要暴露的成员或者方法加载在exports 或者module.exports中

require加载的其实是module.exports导出的对象。

module.exports

let a = 10
const add = (a, b) => a + b
module.exports = { a, add }//支持es6 对象省略写法
console.log(exports);//{}
 

module.exports直接导出函数,require接受的也是这个函数

exports

let a = 10
const add = (a, b) => a + b
exports.a=a
exports.add=add

require

const {a,add}=require('./test4') //直接解构
const test4=require('./test4') //导入module.exports导出的对象

 
 

require exports module.exports的更多相关文章

  1. 【前端学习笔记】2015-09-09~~~~nodejs中的require()和module.exports

    nodejs中一个js文件就可以看做是一个模块 在node环境中,可以直接var a=require('模块路径以及不带扩展名的模块名') exports---module.exports 其中nod ...

  2. Node的require和module.exports

    node编程中最重要的思想之一就是模块,在 Node.js 模块系统中,每个文件都被视为独立的模块.这是这个思想,让javascript的大规模工程成为可能.模块化编程在前端大肆盛行,在node中导出 ...

  3. exports module.exports export export default之间的关系

    exports 和module.exports是CommonJS模块规范 export export default是ES6模块的规范,两者完全是不同的概念. node应用由模块组成,采用的是Comm ...

  4. module.exports,exports,export和export default,import与require区别与联系【原创】

    还在为module.exports.exports.export和export default,import和require区别与联系发愁吗,这一篇基本就够了! 一.首先搞清楚一个基本问题: modu ...

  5. 探讨ES6的import export default 和CommonJS的require module.exports

    今天来扒一扒在node和ES6中的module,主要是为了区分node和ES6中的不同意义,避免概念上的混淆,同时也分享一下,自己在这个坑里获得的心得. 在ES6之前 模块的概念是在ES6发布之前就出 ...

  6. Node.js中exports,module.exports以及require方法

    在Node.js中,使用module.exports.f = ...与使用exports.f = ...是一样的,此时exports就是module.exports的一种简写方式.但是,需要注意的是, ...

  7. module.exports,exports,export和export default,import与require区别与联系

    还在为module.exports.exports.export和export default,import和require区别与联系发愁吗,这一篇基本就够了! 一.首先搞清楚一个基本问题: modu ...

  8. export,export default,module.exports,import,require之间的区别和关联

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

  9. nodejs模块中exports和module.exports的区别

    通过Node.js的官方API可以看到Node.js本身提供了很多核心模块 http://nodejs.org/api/ ,这些核心模块被编译成二进制文件,可以require('模块名')去获取:核心 ...

随机推荐

  1. vue-cookies 使用

    import VueCookies from 'vue-cookies' Vue.use(VueCookies) $cookies.config() 设置默认值 $cookies.config(exp ...

  2. 搭建 springboot selenium 网页文件转图片环境

    1. 环境准备 需要有 chrome 浏览器 + chrome driver + selenium 客户端 离线 chrome 下载地址 # 64位 linux 系统 https://dl.googl ...

  3. git使用-远程仓库(github为例)

    1.登录github(没有先注册账号) 2.settings>SSH and GPG keys>New SSH key Title(自己填写即可) key需要git命令生成 ssh-key ...

  4. 方法解析之Method与ConstMethod介绍

    HotSpot通过Method与ConstMethod来保存方法元信息. 1.Method Method没有子类,定义在method.hpp文件中,其类继承关系如下图: Method用于表示一个Jav ...

  5. 【Mysql】SpringBoot_2.1.0+Druid_1.1.10 配置数据源监控服务Yml格式

    访问地址:localhost:8080/druid 按照这个方法和版本配置没问题 版本或高或低可能会出现不兼容 1.添加依赖 <dependency> <groupId>com ...

  6. C#LeetCode刷题之#496-下一个更大元素 I(Next Greater Element I)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4026 访问. 给定两个没有重复元素的数组 nums1 和 num ...

  7. OptaPlanner的新约束表达方式 Constraint Streams

    有好些时间没有写过关于OptaPlanner的东西了,其实近半年来,OptaPlanner还是推出了不少有用.好用的新特性.包括本文讲到的以Stream接口实现评分编程.关于OptraPlanner的 ...

  8. getting session bus failed: //bin/dbus-launch terminated abnormally with the following error: Autolaunch error: X11 initialization failed.

    今天在调试dbus程序的时候,运行程序出现了getting session bus failed: //bin/dbus-launch terminated abnormally with the f ...

  9. eclipse git如何切换分支,拉取代码,合并代码,解决冲突等

    (如果想看eclipse拉取git项目,移步到我上一篇文章)以下步骤是eclipse运用git的切换分支,拉取合并代码的基本操作: 1.切换远程分支:鼠标右键项目--team--switch to - ...

  10. git 生成并添加 SSH key

    git config --global user.name "wangjunqiang" git config --global user.email "wangjunq ...