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
# v12.18.0
$ uname -a
# Darwin xgqfrms-mbp.local 19.5.0 Darwin Kernel Version 19.5.0: Tue May 26 20:41:44 PDT 2020; root:xnu-6153.121.2~2/RELEASE_X86_64 x86_64
.mjs
& "type": "module",
https://nodejs.org/api/esm.html#esm_enabling
https://nodejs.org/api/esm.html#esm_package_json_type_field
https://nodejs.org/api/esm.html#esm_input_type_flag
.mjs
// package.json
{
"type": "module"
}
--experimental-modules
# node --experimental-modules
# must include `.mjs` file extension name
$ node --experimental-modules esm-node.mjs
$ node --experimental-modules --input-type=module ./esm-node.mjs
.js
bug
// .js
import { a } from "./esm-modules";
import { b } from "./esm-modules";
// import { c } from "./esm-modules";
// import { d } from "./esm-modules";
import esm from "./esm-modules.mjs";
const log = console.log;
log(`a =`, a)
log(`b =`, b)
log(`c =`, esm.c)
log(`d =`, esm.d)
.mjs
solution
you must import es module with the
.mjs
file extension name
// defualt `.js`
/*
import { a } from "./esm-modules";
import { b } from "./esm-modules";
import { c } from "./esm-modules";
import { d } from "./esm-modules";
*/
// .mjs
import { a } from "./esm-modules.mjs";
import { b } from "./esm-modules.mjs";
// import { c } from "./esm-modules.mjs";
// import { d } from "./esm-modules.mjs";
import esm from "./esm-modules.mjs";
module.exports & exports
CJS Modules
// CJS Modules
/*
exports.a = 1;
exports.b = 2;
exports.c = 3;
exports.d = 4;
// exports = { a: 1, b: 2, c: 3, d: 4 }
*/
exports.a = 1;
exports.b = 2;
module.exports = { c: 3 };
module.exports.d = 4;
// module.exports = { c: 3, d: 4 }
结论
exports
just shorthand ofmodule.exports
if exist both
exports
&module.exports
,module.exports
will be overwrittenexports
️best practice, just using
module.exports
as possible as you can
exports.a = 1;
exports.b = 2;
module.exports = { c: 3 };
module.exports.d = 4;
// module.exports = { c: 3, d: 4 }
console.log(module);
exports.a = 1;
exports.b = 2;
module.exports = { c: 3 };
module.exports.d = 4;
// module.exports = { c: 3, d: 4 }
module.exports.hello = true; // Exported from require of module
exports = { hello: false }; // Not exported, only available in the module
console.log(`hello =`, exports);
console.log(module);
https://www.hacksparrow.com/nodejs/exports-vs-module-exports.html
export default & export
ESM Modules
/* eslint-disable no-unused-vars */
// eslint-disable-next-line no-unused-vars
const a = 1;
const b = 1;
const c = 1;
const d = 1;
const esm = {
// a,
// b,
c,
d,
};
export {
a,
b,
}
export default esm;
refs
https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export
https://flaviocopes.com/how-to-enable-es-modules-nodejs/
https://2ality.com/2017/09/native-esm-node.html
https://blog.logrocket.com/how-to-use-ecmascript-modules-with-node-js/
https://github.com/nodejs/modules/blob/master/doc/plan-for-new-modules-implementation.md
https://scotch.io/tutorials/new-ecmascript-modules-in-node-v12/amp
xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
node --experimental-modules & node.js ES Modules的更多相关文章
- Node.js & ES Modules & Jest
Node.js & ES Modules & Jest CJS & ESM CommonJS https://en.wikipedia.org/wiki/CommonJS ht ...
- 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] CommonJS Modules
CoomonJS modules provide a clean syntax for importing dependencies. This lesson will take a look at ...
- nohup /usr/local/node/bin/node /www/im/chat.js >> /usr/local/node/output.log 2>&1 &
nohup和&后台运行,进程查看及终止 &后台运行 登出ssh终端,进程会被自动kill掉 但是nohup >>XX.log 2>&1 & 登出终 ...
- node基础篇一:node介绍、node http、node event 课堂(持续)
最近工作一直很忙,没时间更新,谅解,这次准备更新一次node教程,本课堂将持续更新,每周坚持更新一到两章,希望对大家有一些小帮助吧: 一.首先什么是node? 1/Node.js 是一个基于 Chro ...
- Solve Error: node postinstall sh: node: command not found
When install the yeoman using the following command: npm install -g yo You might have the following ...
- node.js02 安装Node环境
安装Node环境 在node.js01中我大概了解了什么是node.js,这次进入起步阶段,首先要安装下Node环境. 开始安装 查看当前Node环境的版本号 win+r输入cmd进入命令行,输入no ...
- 获取所有树叶子节点 注册添加事件 if ($(node).tree('isLeaf', node.target)) 是否叶子节点
//获取所有树叶子节点 注册添加事件 if ($(node).tree('isLeaf', node.target)) 是否叶子节点 $(function () { $('.easyui-tree') ...
- elasticsearch节点(角色)类型解释node.master和node.data
在生产环境下,如果不修改elasticsearch节点的角色信息,在高数据量,高并发的场景下集群容易出现脑裂等问题. 默认情况下,elasticsearch集群中每个节点都有成为主节点的资格,也都存储 ...
随机推荐
- SQLyog破解30天到期
开始--运行中输入regedit.找到regedit.exe 文件 点击regedit.exe...就把注册表编辑器打开了 我们需要找到记录软件使用实现的数据...找到HKEY-CURRENT ...
- 圣诞快乐!OIer挂分小技巧
OIer常犯错误 自己的错误 循环里套return 线段树求和 int 定义,下传 int 定义 cmp<,>号分不清 主观行为举动错误 踢电源线,注意安全(_Destiny) TLE 大 ...
- day133:2RenMJ:TypeScript的变量&函数&类&接口
目录 1.变量 2.函数 3.类 4.接口 1.变量 1.变量的声明 // 1.即指定数据类型 也指定值 var 变量名:类型 = 值; eg:var username:string = " ...
- 十一:SpringBoot-事务管理
SpringBoot-事务管理 1.事务管理简介 1.1 特性:ACID 1.2 隔离问题 1.3 隔离级别 2.Spring事务管理 2.1 顶级接口 2.2 事务状态 2.3 事务定义 1.事务管 ...
- Jenkins (自动使用docker容器发布java.war +tomcat)
一.大概流程 因为目前没有找Jenkins 和docker 之间比较友好的插件,所以只能使用这种比较low 的方式来实现自动部署了. 1.Jenkins在gitlab拉取项目并编译. 2.将编译后的代 ...
- cocos2d-x 调试问题
1.昨天一个新功能,在xcode模拟器上测试没问题.后来打包安卓后,一直有问题 就又添加日志功能 # define CCLOGFUNC(s) ...
- java 生成xml
生成xml @Test public void parseApplicationConfigXML(){ try { SAXReader sax = new SAXReader(); Document ...
- Codeforces Round #656 (Div. 3) A. Three Pairwise Maximums
题目链接:https://codeforces.com/contest/1385/problem/A 题意 给出三个正整数 $x,y,z$,找出三个正整数 $a,b,c$ 使得 $x = max(a, ...
- Codeforces Round #627 (Div. 3) B - Yet Another Palindrome Problem(逻辑)
题意: 问一个数组中是否存在至少长为3的回文子数组(按下标排列,可不连续). 思路: 找三个相同数或两个不连续的相同数. #include <bits/stdc++.h> using na ...
- c语言实现--顺序表操作
经过三天的时间终于把顺序表的操作实现搞定了.(主要是在测试部分停留了太长时间) 1;线性表顺序存储的概念:指的是在内存中用一段地址连续的存储单元依次存储线性表中的元素. 2;采用的实现方式:一段地址连 ...