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 }

结论

  1. exports just shorthand of module.exports

  2. if exist both exports & module.exports, module.exports will be overwritten exports

  3. 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.sitepoint.com/understanding-module-exports-exports-node-js#whatsthedifferencebetweenmoduleexportsandexports

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的更多相关文章

  1. Node.js & ES Modules & Jest

    Node.js & ES Modules & Jest CJS & ESM CommonJS https://en.wikipedia.org/wiki/CommonJS ht ...

  2. Node.js & ES modules & .mjs

    Node.js & ES modules & .mjs Node.js v13.9.0 https://nodejs.org/api/esm.html https://nodejs.o ...

  3. [Node.js] CommonJS Modules

    CoomonJS modules provide a clean syntax for importing dependencies. This lesson will take a look at ...

  4. 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 & 登出终 ...

  5. node基础篇一:node介绍、node http、node event 课堂(持续)

    最近工作一直很忙,没时间更新,谅解,这次准备更新一次node教程,本课堂将持续更新,每周坚持更新一到两章,希望对大家有一些小帮助吧: 一.首先什么是node? 1/Node.js 是一个基于 Chro ...

  6. 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 ...

  7. node.js02 安装Node环境

    安装Node环境 在node.js01中我大概了解了什么是node.js,这次进入起步阶段,首先要安装下Node环境. 开始安装 查看当前Node环境的版本号 win+r输入cmd进入命令行,输入no ...

  8. 获取所有树叶子节点 注册添加事件 if ($(node).tree('isLeaf', node.target)) 是否叶子节点

    //获取所有树叶子节点 注册添加事件 if ($(node).tree('isLeaf', node.target)) 是否叶子节点 $(function () { $('.easyui-tree') ...

  9. elasticsearch节点(角色)类型解释node.master和node.data

    在生产环境下,如果不修改elasticsearch节点的角色信息,在高数据量,高并发的场景下集群容易出现脑裂等问题. 默认情况下,elasticsearch集群中每个节点都有成为主节点的资格,也都存储 ...

随机推荐

  1. hadoop及NameNode和SecondaryNameNode工作机制

    hadoop及NameNode和SecondaryNameNode工作机制 1.hadoop组成 Common MapReduce Yarn HDFS (1)HDFS namenode:存放目录,最重 ...

  2. https://nginx.org/en/docs/http/request_processing.html

    https://nginx.org/en/docs/http/request_processing.html

  3. mysqldump 内存消耗

    MySQL :: MySQL 8.0 Reference Manual :: 4.5.4 mysqldump - A Database Backup Program https://dev.mysql ...

  4. websocket心跳重连 websocket-heartbeat-js

    初探和实现websocket心跳重连(npm: websocket-heartbeat-js) 心跳重连缘由 websocket是前后端交互的长连接,前后端也都可能因为一些情况导致连接失效并且相互之间 ...

  5. 目前 c++ primer学习总结笔记

    C++的开始 1 main的返回值:0为成功状态,非0为系统定义的错误类型 2 输入输出:计算结果为左侧运算对象,IO操作读写缓冲与程序中的动作无关 3 输入流istream对象:cin(标准输入): ...

  6. Spring,Spring MVC,MyBatis,Hibernate总结

    将之前学习的框架知识进行了UML图总结,若有错误或不当之处,劳烦朋友们指正,会及时作出修改和补充: [toc] Spring Spring MVC MyBatis,Hibernate

  7. form(form基础、标签渲染、错误显示 重置信息、form属性、局部钩子、全局钩子)

    form基础 Django中的Form使用时一般有两种功能: 1.生成html标签 2.验证输入内容 要想使用django提供的form,要在views里导入form模块 from django im ...

  8. python--基础2 (数据类型及应用)

    资源池 链接:https://pan.baidu.com/s/1OGq0GaVcAuYEk4F71v0RWw 提取码:h2sd python数据类型 字符串 列表 字典 数字(整数) 数字(浮点数) ...

  9. 小白搭建WAMP详细教程---php安装与设置

    一.php官网下载php压缩包 到php官网http://www.php.net 下载,有很多版本,我们这里选择7.2.25,具体步骤如下: 二.php的安装 下载后得到如下的压缩包,将压缩包解压到您 ...

  10. 敏捷史话(五):敏捷已逝 —— Dave Thomas

    " 敏捷已逝,但敏捷精神长存.因为所谓的敏捷专家卖给你的是方法论,而不是价值."当多数人都在从"敏捷"身上榨取利益时, Dave Thomas 成为了一位逆行者 ...