CoomonJS modules provide a clean syntax for importing dependencies. This lesson will take a look at the basics of using CommonJS modules.

app.js

var dep = require('./dep');

console.log(dep); // Exports a string back

dep.js

module.exports =  "Exports a string back";

You can exports anything, such as a function:

app.js

var dep = require('./dep');

console.log(dep()); // Export a function back

dep.js

module.exports = function() {
return "Exports a function back";
}

Exprots multi-value:

app.js

var dep = require('./dep');

console.log(dep.foo, dep.bar); //foo, bar

dep.js

module.exports = {
foo: "foo",
bar: "bar"
}

Normally, you should do like this, using exprots object directly:

dep.js

exports.foo = "foo";
exports.bar = "bar";

[Node.js] CommonJS Modules的更多相关文章

  1. Node.js & ES Modules & Jest

    Node.js & ES Modules & Jest CJS & ESM CommonJS https://en.wikipedia.org/wiki/CommonJS ht ...

  2. node --experimental-modules & node.js ES Modules

    node --experimental-modules & node.js ES Modules how to run esm modules in node.js cli $ node -v ...

  3. Node.js & ES modules & .mjs

    Node.js & ES modules & .mjs Node.js v13.9.0 https://nodejs.org/api/esm.html https://nodejs.o ...

  4. [Node.js] 05 - Modules and Function

    一个 Node.js 文件就是一个模块,这个文件可能是JavaScript 代码.JSON 或者编译过的C/C++ 扩展. 模块是Node.js 应用程序的基本组成部分,文件和模块是一一对应的. No ...

  5. [Node.js] Exporting Modules in Node

    In this lesson, you will learn the difference between the exports statement and module.exports. Two ...

  6. [Node.js] 4. Modules

    4.2 Missing Exports Notice the two different files: high_five.js on the left side andapp.js on the r ...

  7. Node.js学习 - Modules

    创建模块 当前目录:hello.js, main.js // hello.js exports.world = function() { // exports 对象把 world 作为模块的访问接口 ...

  8. Node.js 开发模式(设计模式)

    Asynchronous code & Synchronous code As we have seen in an earlier post (here), how node does th ...

  9. Node.js require 模块加载原理 All In One

    Node.js require 模块加载原理 All In One require 加载模块,搜索路径 "use strict"; /** * * @author xgqfrms ...

随机推荐

  1. Strider SSH Deploy配置

    登录需要ssh, ssh 免密码登录配置自行百度.shell里写成自己的需要的命令

  2. js 发红包

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  3. 转】mysql数据库delete数据时不支持表别名

    原博文出自于: http://www.cnblogs.com/xdp-gacl/p/4012853.html 感谢! 今天在帮同事查看一条删除的SQL语句执行出错的问题 SQL语句如下: 1 DELE ...

  4. c#与vb.net在App_Code里面编译要通过,需要以下web.config的配置

    web.config的配置: <system.web> <codeSubDirectories> <add directoryName="VB"/&g ...

  5. SQL获取变量类型以及变量最大长度

    DECLARE @Temp nvarchar(1050)='' SELECT CAST(SQL_VARIANT_PROPERTY(@Temp, 'BaseType') AS VARCHAR(50))S ...

  6. python知识点(07-08)

    python: 循环else: while true: if x>1: print() break else: print() 文件循环: for line in open(‘test.txt’ ...

  7. flask中的request对象方法

    'accept_charsets','accept_encodings','accept_languages','accept_mimetypes','access_route','applicati ...

  8. emWin(ucGui)的Edit控件退格处理方法 worldsing

    在enWin(ucGui)中EDIT控件在数值模式(十进制/十六进制/二进制/浮点数)下编辑是,无法使用BackSpace键进行退格,主要涉及到的函数有: EDIT_SetBinMode() EDIT ...

  9. [置顶] 我的设计模式学习笔记------>Java设计模式总概况

    设计模式的概念最早起源于建筑设计大师Alexander的<建筑的永恒方法>一书,尽管Alexander的著作是针对建筑领域的,但是他的观点实际上用用于所有的工程设计领域,其中也包括软件设计 ...

  10. [网络]远程访问局域网svn服务器[转]

    转至:http://8474832.blog.51cto.com/8464832/1555449 打开路由器访问界面 选择转发规则->端口映射-新建 在弹出的界面中填写相应的端口号了内网ip 填 ...