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. Monkey patching

    "A monkey patch is a way to extend or modify the run-time code of dynamic languages without alt ...

  2. (Oracle)误删oracle表结构恢复

    在操作数据库时,我们常常会不小心把表结构删除了.有时候建表很麻烦大到100多个字段,而又找不到当初的建表语句.其实这时候不用担心,oracle和咱们widows一样,他也有个回收站,只要你没有清除回收 ...

  3. 分布式跟踪的一个流行标准是OpenTracing API,该标准的一个流行实现是Jaeger项目。

    https://github.com/jaegertracing/jaeger https://mp.weixin.qq.com/s/-Tn2AgyHoq8pwMun8JHcGQ Jaeger的深入分 ...

  4. 为什么要选择学习Java?适合零基础的初学者的文章

    我经常收到这样的问题:"要学习的第一门编程语言是什么?" Java是一门好的编程语言吗?"和" Java是适合初学者的好的第一门编程语言,还是我应该从Java或 ...

  5. 单机模拟配置Eureka集群

    首先先提醒单机部署的重要点 如果使用一个ip地址(适用于单网卡)每个eureka实例使用不同的域名映射到同一个IP 如果每个eureka实例使用不同的IP(多网卡),要确保这些IP要都表示本地 本文假 ...

  6. Spring Boot:定时任务与图片压缩处理

    一.定时任务 1.创建定时任务 2.@Scheduled 二.图片压缩处理 1.添加thumbnailator依赖 2.创建图片处理类 3.基本使用方法 一.定时任务 项目中可以采用定时任务进行一些操 ...

  7. Spring5源码,Spring DispatecherServlet的生命周期

    一.前端控制器模式 二.DispatcherServlet的执行链 三.DispatcherServlet 1.策略初始化 2.请求预处理 3.请求处理 4.视图解析 5.处理调度请求 - 视图渲染 ...

  8. Linux 使用Vmware克隆,修改克隆机器内容

    在Vmware中搭建好一台虚拟机,拍照快照,然后克隆其他集群进行练习,克隆后的机器都需要修改的内容有如下几点: 1:各机器之间,在网络上能够互相ping通 每台机器的IP地址必须是唯一的. 进入 ca ...

  9. python --装饰器通俗讲解

    装饰器 什么是装饰器?:在不修改源代码和调用方式的基础上给其增加新的功能,多个装饰器可以装饰在同一个函数上 Python中的装饰器是你进入Python大门的一道坎; 装饰器特点: 不改变原函数原代码: ...

  10. 设计模式(二)——Java简单工厂模式

    简单工厂模式 案例: 披萨的项目(要便于披萨种类的扩展,要便于维护) 1)披萨的种类很多(比如 GreekPizz.CheesePizz 等) 2)披萨的制作有 prepare,bake, cut, ...