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
.mjsfile 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 }
结论
exportsjust shorthand ofmodule.exportsif exist both
exports&module.exports,module.exportswill be overwrittenexports️best practice, just using
module.exportsas 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集群中每个节点都有成为主节点的资格,也都存储 ...
随机推荐
- jmeter报Address already in use: connect
jmeter报Address already in use: connect 用windows进行jmeter压测出现java.net.BindException: Address already ...
- 知乎社区核心业务 Golang 化实践 - 知乎 https://zhuanlan.zhihu.com/p/48039838
知乎社区核心业务 Golang 化实践 - 知乎 https://zhuanlan.zhihu.com/p/48039838
- get_or_create update_or_create
django/query.py at master · django/django https://github.com/django/django/blob/master/django/db/mod ...
- C++ Primer Plus读书笔记(九)内存模型和名称空间
1.作用域和链接 int num3; static int num4; int main() { } void func1() { static int num1; int num2; } 上边的代码 ...
- ACCESS数据库偏移注入
偏移注入主要是针对知道表,但是不知道字段的ACCESS数据库. 比如我们已经知道了表名是 admin 判断字段数: http://192.168.74.136:8002/Production/PROD ...
- SpringBoot启动方式,Spring Boot 定义系统启动任务
SpringBoot启动方式,Spring Boot 定义系统启动任务 SpringBoot启动方式 1.1 方法一 1.2 方法二 1.2.1 start.sh 1.2.2 stop.sh 1.2. ...
- redis-服务器配置-主从
1.配置sentinel.conf -------------------------------------------------- port 26379 dir "/home/app/ ...
- checkbox限制选中个数
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- 微信支付V3 SDK Payment Spring Boot 1.0.6 发布,实现留守红包,助力抗疫
春节将至,为防控疫情,多地政府提倡员工.外来务工者留守本地过年,并鼓励企业向员工发放"留守红包".为此,微信支付推出"春节留守红包"能力,希望可以协助有发放需求 ...
- python+selenium+bs4爬取百度文库内文字 && selenium 元素可以定位到,但是无法点击问题 && pycharm多行缩进、左移
先说一下可能用到的一些python知识 一.python中使用的是unicode编码, 而日常文本使用各类编码如:gbk utf-8 等等所以使用python进行文字读写操作时候经常会出现各种错误, ...