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集群中每个节点都有成为主节点的资格,也都存储 ...
随机推荐
- uni-app请求uni.request封装使用
对uni.request的一些共同参数进行简单的封装,减少重复性数据请求代码.方便全局调用. 先在目录下创建 utils 和 common 这2个文件夹 utils 是存放工具类的,common 用来 ...
- ETL调优的一些分享(下)(转载)
如在上篇文章<ETL调优的一些分享(上)>中已介绍的,ETL是构建数据仓库的必经一环,它的执行性能对于数据仓库构建性能有重要意义,因此对它进行有效的调优将十分重要.ETL业务的调优可以从若 ...
- C++ Primer Plus读书笔记(二)处理数据
1.格式化输出: 和C语言不太一样,C++格式化输出进制格式如下: 1 int a = 42; 2 int b = 42; 3 int c = 42; 4 5 cout << a < ...
- BIO,NIO,AIO 总结
BIO,NIO,AIO 总结 Java 中的 BIO.NIO和 AIO 理解为是 Java 语言对操作系统的各种 IO 模型的封装.程序员在使用这些 API 的时候,不需要关心操作系统层面的知识,也不 ...
- 时间模块,os模块,sys模块
时间模块 和时间有关系的我们就要用到时间模块.在使用模块之前,应该首先导入这个模块. #常用方法 1.time.sleep(secs) (线程)推迟指定的时间运行.单位为秒. 2.time.time( ...
- 令牌桶、漏斗、冷启动限流在sentinel的应用
分布式系统为了保证系统稳定性,在服务治理的限流中会根据不同场景进行限流操作,常见的限流算法有: 令牌桶:可容忍一定突发流量的速率的限流,令牌桶算法的原理是系统以恒定的速率产生令牌,然后把令牌放到令牌桶 ...
- MySql(三)存储过程和函数
MySql(三)存储过程和函数 一.什么是存储过程和函数 二.存储过程和函数的相关操作 一.什么是存储过程和函数 存储过程和函数是事先经过编译并存储在数据库中的一段SQL语句的集合,调用存储过程和函数 ...
- Mysql 基本操作命令
1.添加用户 1.1 登录MYSQL: @>mysql -u root -p @>密码 1.2 创建用户: 格式:grant select on 数据库.* to 用户名@登录主机 ide ...
- 深入理解nodejs的HTTP处理流程
目录 简介 使用nodejs创建HTTP服务 解构request 处理Request Body 处理异常 解构response 简介 我们已经知道如何使用nodejs搭建一个HTTP服务,今天我们会详 ...
- Java并发包源码学习系列:基于CAS非阻塞并发队列ConcurrentLinkedQueue源码解析
目录 非阻塞并发队列ConcurrentLinkedQueue概述 结构组成 基本不变式 head的不变式与可变式 tail的不变式与可变式 offer操作 源码解析 图解offer操作 JDK1.6 ...