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. 单独删除std::vector <std::vector<string> > 的所有元素

    下面为测试代码: 1.创建 std::vector< std::vector<string> > vc2; 2.初始化 std::vector<string> vc ...

  2. 说说Python 中的文件操作 和 目录操作

    我们知道,文件名.目录名和链接名都是用一个字符串作为其标识符的,但是给我们一个标识符,我们该如何确定它所指的到底是常规文件文件名.目录名还是链接名呢?这时,我们可以使用os.path模块提供的isfi ...

  3. 2014搜狗前端面经【B事业部】

    本来就投了一份简历,后来又收到了个B事业部的面试电话,今天刚面完一面,总体感觉还是很基础的,其中一名面试官帅到不行啊!另一个也不差,真是幸胡...(sorry,跑题了...) 上来先做了份笔试题,超级 ...

  4. 【boost】使用lambda表达式和generate_n生成顺序序列

    程序中经常用到顺序序列(0,1,2,3,4,5,6.....),一直羡慕python有range这样的函数,而C++中通常只有用循环来处理这种初始化. 现在,结合boost库lambda(虽然差C++ ...

  5. QCon 2013 上海 -- 高并发可用

      高并发可用应该是这次QCon的主要议题,目测超过一半的话题都在讨论这个主题或者和这个主题相关.看到Yun关于AWS re:Invent的总结,好像这个在AWS上也是很热的一个主题.就我个人而言,没 ...

  6. URAL-1998 The old Padawan 二分

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1998 题意:有n个石头,每个石头有个重量,每个时间点你能让一个石头飞起来,但有m个时间点 ...

  7. 第二百三十二天 how can I 坚持

    早上竟然飘起了大学,可是就下了一会,没有一点学的痕迹. 博客园真不知道怎么回事了,字真的好小了. 晚上回来心情好不好,感觉好累,最近不知道怎么了,约罗娜出来吃个饭怎么都约不出来,咋办呢.哎,愁人. 最 ...

  8. 触控发布《Cocos开发者平台白皮书》

    Cocos 2014 开发者大会(秋季)组委会今天正式发布了<Cocos开发者平台白皮书>,GameRes游资网得到Cocos官方授权发布该白皮书电子版. 白皮书主要内容包括对行业的趋势解 ...

  9. POJ 1066 Treasure Hunt(线段相交判断)

    Treasure Hunt Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4797   Accepted: 1998 Des ...

  10. RedHat/CentOS6.4---永久关闭iptables

    今天无意中发现一个现象,当我关闭iptables并且停止iptables服务,但是总会有一些出奇的事情发生,当我再次启动系统,查看iptables状态,iptables又自动开启,很是无奈啊!在Red ...