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集群中每个节点都有成为主节点的资格,也都存储 ...
随机推荐
- LOJ10162 骑士
ZJOI 2008 Z 国的骑士团是一个很有势力的组织,帮会中聚集了来自各地的精英.他们劫富济贫,惩恶扬善,受到了社会各界的赞扬. 可是,最近发生了一件很可怕的事情:邪恶的 Y 国发起了一场针对 Z ...
- python 中excel表格的操作【转载】
传说中python操作ms office功能最强大的是win32com,但只能要ms上使用. 不过对于比较简单的需求显得有些小题大作.那么来看下简单的,分别是xlrd和xlwt模块, 不过暂时只支持e ...
- js基础(使用Canvas画图)
HTML5的元素提供了一组JavaScript API,让我们可以动态地创建图形和图像. 图形是在一个特定的上下文中创建的,而上下文对象目前有两种.第一种是2D上下文,可以执行原始的绘图操作, 比如: ...
- SpringMvc获取getbean
import org.apache.commons.lang3.Validate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; i ...
- cassandra权威指南读书笔记--cassandra查询语言
cassandra使用一个特殊主键(复合键)表示宽行,宽行也叫分区.复合键由一个分区键和一组可选的集群列组成.分区键用于确定存储行的节点,分区键也可以包含多个列.集群键用于控制数据如何排序以及在分区中 ...
- docker(9)Dockerfile制作镜像
前言 如果我们已经安装了一个python3的环境,如果另一台机器也需要安装同样的环境又要敲一遍,很麻烦,这里可以配置Dockerfile文件,让其自动安装,类似shell脚本 Dockerfile编写 ...
- docker(2)CentOS 7安装docker环境
前言 前面一篇学了mac安装docker,这篇来学习在linux上安装docker 环境准备 Docker支持以下的CentOS版本,目前,CentOS 仅发行版本中的内核支持 Docker. Doc ...
- VS CODE远程办公篇一
作者:良知犹存 转载授权以及围观:欢迎添加微信:becom_me 总述 因为疫情的来临,让远程办公变得原来越火.这次我也是盯上了这个功能,实现在家里远程配置电脑. 嵌入式Linux开发的程序员 ...
- Codeforces Round #628 (Div. 2) C. Ehab and Path-etic MEXs(树,思维题)
题意: 给有 n 个点的树的 n-1 条边从 0 到 n-2 编号,使得任意两点路径中未出现的最小数最小的方案. 思路: 先给所有度为 1 的点所在边编号,之后其他点可以随意编排. #include ...
- P3384 [模板] 树链剖分
#include <bits/stdc++.h> using namespace std; typedef long long ll; int n, m, rt, mod, cnt, to ...