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. 初识-----基于Socket的UDP和TCP编程及测试代码

    一.概述 TCP(传输控制协议)和UDP(用户数据报协议是网络体系结构TCP/IP模型中传输层一层中的两个不同的通信协议. TCP:传输控制协议,一种面向连接的协议,给用户进程提供可靠的全双工的字节流 ...

  2. javascript对象事件绑定方法

    javascript对象事件绑定方法 今天在做对象事件绑定的过程中出现了一点异外情况,由于事件方法是由参数传过来的,需要将当前对象call过去,方便方法体里直接调用this 错误写法 obj.oncl ...

  3. Google软件测试

    google测试相关的职位有三类:软件测试开发工程师.测试工程师以及测试工程经理. 软件测试开发工程师也是一个开发角色,只是工作重心在可测试性和通用测试框架上.他们参与设计评审,非常近距离地观察代码质 ...

  4. TopFreeTheme精选免费模板【20130619】

    今天给大家推荐7款最新精选的WordPress主题和一个WooCommerce订单跟踪插件,如果你有更换自己博客主题的想法或者正要做自己的博客,不妨试试.一些是WordPress商业模板,但都可以免费 ...

  5. IOS列表实现动态多列

    . //图片列表 NSMutableArray *pictureList; //分组列表 NSMutableArray *indexArr; - (UITableViewCell *)tableVie ...

  6. 设置UINavigationController相同标题

    设置UINavigationController相同标题,让UINavigationController内的每一个ViewController的标题都一样,可以使用以下设置. UINavigation ...

  7. java getEnv不区分大小写 getProperty区分大小写

    System.out.println(System.getenv("JAVA_HOME")); System.out.println(System.getenv("Pat ...

  8. POJ3630Phone List(字典树)

    经典的字典树的题目了,这次完全是按照自己的风格来写的,没有参考其他人的代码风格来写. 分析:如果采用常规的暴力枚举,那么复杂度就是O(n*n*str.length) = O(10^9),这明显是会超时 ...

  9. URAL 2073 Log Files (模拟)

    题意:给定 n 场比赛让你把名称,时间,比赛情况按要求输出. 析:很简单么,按照要求输出就好,注意如果曾经AC的题再交错了,结果也是AC的. 代码如下: #pragma comment(linker, ...

  10. ajax 本地测试,使用Chrome 浏览器

    出现问题: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is ...