[Node.js] CommonJS Modules
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的更多相关文章
- Node.js & ES Modules & Jest
Node.js & ES Modules & Jest CJS & ESM CommonJS https://en.wikipedia.org/wiki/CommonJS ht ...
- 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 ...
- Node.js & ES modules & .mjs
Node.js & ES modules & .mjs Node.js v13.9.0 https://nodejs.org/api/esm.html https://nodejs.o ...
- [Node.js] 05 - Modules and Function
一个 Node.js 文件就是一个模块,这个文件可能是JavaScript 代码.JSON 或者编译过的C/C++ 扩展. 模块是Node.js 应用程序的基本组成部分,文件和模块是一一对应的. No ...
- [Node.js] Exporting Modules in Node
In this lesson, you will learn the difference between the exports statement and module.exports. Two ...
- [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 ...
- Node.js学习 - Modules
创建模块 当前目录:hello.js, main.js // hello.js exports.world = function() { // exports 对象把 world 作为模块的访问接口 ...
- Node.js 开发模式(设计模式)
Asynchronous code & Synchronous code As we have seen in an earlier post (here), how node does th ...
- Node.js require 模块加载原理 All In One
Node.js require 模块加载原理 All In One require 加载模块,搜索路径 "use strict"; /** * * @author xgqfrms ...
随机推荐
- JDK - Tomcat - JSP - Servlet 配置运行全攻略(转)
http://www.cnblogs.com/myqiao/archive/2005/08/29/225497.html 花了将近两个月的时间,从 JDK 开始一步一步摸索,历经千辛万苦,终于让第一个 ...
- 【LeetCode】83 - Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- 刚刚大学毕业,自己搭网站遇到的问题 一:tomcat中同时部署两个项目的问题
最近直接把两个项目打成war包在tomcat下发布,出现了很多莫名奇妙的问题,就是不能发布成功,只能有一个项目能成功,在网上查了很多方法,以为是两个项目中jar包出现冲突,也按照网上的方法把两个项目中 ...
- js中location.search、split()HTML5中localStorage
1. location.search在客户端获取Url参数的方法 location.search是从当前URL的?号开始的字符串 如:http://www.baidu.com/s?wd=baidu&a ...
- 使用Powershell 添加,选择更改订阅
PS C:\WINDOWS\system32> Import-AzurePublishSettingsFile 'C:\Users\Ling\Desktop\Free-11-24-2014-cr ...
- 使用ReflectionTestUtils解决依赖注入
概述 当使用junit来测试Spring的代码时,为了减少依赖,需要给对象的依赖,设置一个mock对象,但是由于Spring可以使用@Autoware类似的注解方式,对私有的成员进行赋值,此时无法 ...
- 随手记录一个 firefox的backgroundPostion-x和-y的问题
今天帮大师写了一天项目,后来在测试一个显示升序和降序的标签上面,我使用了一个backgroundPosition-y来判断当前icon的状态,却无法不管是使用闭包还是个钟手段,在 firefox下面总 ...
- 第三百四十三天 how can I 坚持
今天又莫名其妙的烦起来了,好没劲. 现在还在看电视机<太阳的后裔>,晚上也没怎么吃饭,干吃了两个馒头,老干妈+生洋葱,好凄惨. 上班看了好长时间会,乱七八糟的. 坚决不跳槽,但得坚持自己的 ...
- 【转】Hive学习路线图
原文博客出自于:http://blog.fens.me/hadoop-hive-roadmap/ 感谢! Hive学习路线图 Hadoop家族系列文章,主要介绍Hadoop家族产品,常用的项目包括Ha ...
- c++面试题总结(1)
1.int a=5,则 ++(a++)的值是() A.5 B. 6 C.7 D.逻辑错误 a++返回的是一个临时变量,这里是右值,不能再前面++了 2.下面 ...