[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 ...
随机推荐
- java中的类实现comparable接口 用于排序
import java.util.Arrays; public class SortApp { public static void main(String[] args) { Student[] s ...
- leetcode:Roman to Integer(罗马数字转化为罗马数字)
Question: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the rang ...
- 设计模式 单件-Singleton
单件模式 Singleton 什么时候使用?当需要独一无二的对象时,请想起他. 举例:线程池(threadpool),缓存(cache),对话框,处理偏好设置和注册表(registry)的对象,驱动程 ...
- 【Python学习笔记】with语句与上下文管理器
with语句 上下文管理器 contextlib模块 参考引用 with语句 with语句时在Python2.6中出现的新语句.在Python2.6以前,要正确的处理涉及到异常的资源管理时,需要使用t ...
- php 解压 .gz 文件
在百度搜索到的 PharData 类和 ZipArchive 都是解压不了 .gz 的文件的,后来在 google 搜索到解决方法,问题解决. try { $phar = new PharData($ ...
- linux 下查找大于100M的文件
命令行如下 find . -type f -size +100M Linux系统下查找大文件或目录的技巧 当硬盘空间不够时,我们就很关心哪些目录或文件比较大,看看能否干掉一些了,怎么才能知道呢?以易读 ...
- 服务器之间建立oracle之间的关联语句
create public database link DBLINK_WZGTAMS CONNECT TO WZGTAMS identified by WZGTAMS using ' (DESCRIP ...
- mongodb的查询方式与sql语句对比
下面是sql和Mongodb对应的一些语法: SQL Statement Mongo Query Language Statement CREATE TABLE USERS (a Number, b ...
- ActiveMQ简介与安装
开源消息总线 支持JMS1.1和J2EE 1.4规范的 JMS Provider实现(持久化,XA消息,事务) 对Spring的支持,ActiveMQ可以很容易内嵌到使用Spring的系统里面去 支持 ...
- 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息--3.5 Delete删除用户]
3.5 Delete删除用户 删除也是通过ObjectID获得对象进行删除 [Authorize] public async Task<ActionResult> Delete(strin ...