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. 【LeetCode】38 - Count and Say

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  2. strcpy()的实现

    看到有一个博客讲的比平时理解的更深入,mark一下:strcpy函数的实现 这里只写平时理解的,三个要点: //strcpy自己实现 char *strcpy(char *dest, const ch ...

  3. 25个CSS3 渐变和动画效果教程

    随着最新版CSS3渐变和动画功能发布,Web开发者在开发的过程中有了更多的选择.实际上,已经有了一些替代的技术,目的都是使网站的建设变得简易,高效和快速.不过CSS3所提供的渐变功能有着显著的优点,特 ...

  4. Flex之HTTPService组件调用

    1.采用<s:HTTPService>标签来实现: <?xml version="1.0" encoding="utf-8"?>< ...

  5. proguard 混淆android代码

    官网 http://proguard.sourceforge.net/#manual/examples.html android 2.3后,新建的project默认就有一个project.proper ...

  6. Educational Codeforces Round 5 A. Comparing Two Long Integers

    A. Comparing Two Long Integers time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  7. ZOJ-3686 A Simple Tree Problem 线段树

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 题意:给定一颗有根树,每个节点有0和1两种值.有两种操作: ...

  8. 消除QQ表情小游戏

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  9. HDU 2859 Phalanx (dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2859 给你一个n*n的矩阵,问你最大的对称度是多少(左下右上为对称线) dp[i][j]表示i行j列元 ...

  10. 关于dll的路径问题

    最近在做一个sdk二次开发的项目,具体是将一个C++开发的SDk用C#将它的API接口全部封装一遍,然后再做一个demo就好了 好不容易封装完了,在使用的时候出了问题.原来SDK中的dll老是加载不到 ...