Node.js & ES Modules & Jest
Node.js & ES Modules & Jest
CJS & ESM

CommonJS
https://en.wikipedia.org/wiki/CommonJS
https://nodejs.org/api/modules.html#modules_modules_commonjs_modules

module wrapper
https://nodejs.org/api/modules.html#modules_the_module_wrapper

ES Modules
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
https://nodejs.org/api/esm.html#esm_modules_ecmascript_modules

Jest
ES5
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-08-0
* @modified
*
* @description
* @difficulty Easy Medium Hard
* @complexity O(n)
* @augments
* @example
* @link
* @solutions
*
*/
const log = console.log;
const sum = (a = 0, b = 0) => {
try {
log(`a, b =`, a, b);
return a + b;
} catch (err) {
throw new Error(`参数错误`, err);
}
};
module.exports = sum;
const sum = require('./sum');
// const sum = require('./sum.js');
test('sum(1, 2) should return 3', () => {
expect(sum(1, 2).toBe(3));
});
ES6
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-08-0
* @modified
*
* @description
* @difficulty Easy Medium Hard
* @complexity O(n)
* @augments
* @example
* @link
* @solutions
*
*/
const log = console.log;
const sum = (a = 0, b = 0) => {
try {
log(`a, b =`, a, b);
return a + b;
} catch (err) {
throw new Error(`参数错误`, err);
}
};
export default sum;
export {
sum,
};
import { sum } from "./sum";
// import { sum } from "./sum.mjs";
test('sum(1, 2) should return 3', () => {
expect(sum(1, 2).toBe(3));
});
module.exports & exports
Node.js
https://www.w3schools.com/nodejs/nodejs_modules.asp
https://www.sitepoint.com/understanding-module-exports-exports-node-js/
https://tc39.es/ecma262/#sec-modules
https://stackoverflow.com/questions/7137397/module-exports-vs-exports-in-node-js
https://github.com/xgqfrms-GitHub/node-express4-restful-api/issues/2
refs
https://flaviocopes.com/how-to-enable-es-modules-nodejs/
http://www.ruanyifeng.com/blog/2020/08/how-nodejs-use-es6-module.html
xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
Node.js & ES Modules & Jest的更多相关文章
- 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 ...
- 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] 05 - Modules and Function
一个 Node.js 文件就是一个模块,这个文件可能是JavaScript 代码.JSON 或者编译过的C/C++ 扩展. 模块是Node.js 应用程序的基本组成部分,文件和模块是一一对应的. No ...
- [Node.js] Exporting Modules in Node
In this lesson, you will learn the difference between the exports statement and module.exports. Two ...
- [Node.js] 4. Modules
4.2 Missing Exports Notice the two different files: high_five.js on the left side andapp.js on the r ...
- [Node.js] CommonJS Modules
CoomonJS modules provide a clean syntax for importing dependencies. This lesson will take a look at ...
- Node.js学习 - Modules
创建模块 当前目录:hello.js, main.js // hello.js exports.world = function() { // exports 对象把 world 作为模块的访问接口 ...
- Node.js require 模块加载原理 All In One
Node.js require 模块加载原理 All In One require 加载模块,搜索路径 "use strict"; /** * * @author xgqfrms ...
- 【nodejs笔记1】配置webstorm + node.js +express + mongodb开发博客的环境
1. 安装webstorm 并破解 2. 安装node (以及express框架) 至官网下载并安装.(http://nodejs.org)v0.10.32 msi 安装后测试,打开命令行, c ...
随机推荐
- 为什么 Go 模块在下游服务抖动恢复后,CPU 占用无法恢复
为什么 Go 模块在下游服务抖动恢复后,CPU 占用无法恢复 https://xargin.com/cpu-idle-cannot-recover-after-peak-load/ 极端情况下收缩 G ...
- 20200927gryz校赛心得
今天gyh学长给我们办了一场校内模拟赛,特地跑来记录一下心得 昨天晚上问了一下lkp学长,听说题目不卡常,不毒瘤,因此我在考试前20分钟仍在若无其事的练习着刚学的强连通分量,丝毫不慌 结果虽然rank ...
- 【DB宝36】使用Docker分分钟搭建漂亮的prometheus+grafana监控
目录 一.部署Prometheus+grafana环境 1.1.下载Prometheus+grafana镜像 1.2.创建镜像 1.3.浏览器访问 二.监控Linux主机 2.1.在被监控主机上部署n ...
- c++复习笔记(3)
这篇是各种琐碎的东西. 类的函数如果在类内部直接实现,则成为内联函数候选.类外部实现的方法,可以用inline声明,使其称为内联函数候选.但是函数是否可以成为内联函数,需要看编译器的行为.. 构造函数 ...
- CS229 Lecture 02
最近忙成狗,各种意义上.第二章其实之前已经看过了但是已经完全忘记了,于是重新看了一遍当复习. 判别学习算法:直接学习$p(y|x)$,或学习一个假设$h_{\theta}(x)$输出结果 生成学习算法 ...
- Hadoop伪分布式环境搭建+Ubuntu:16.04+hadoop-2.6.0
Hello,大家好 !下面就让我带大家一起来搭建hadoop伪分布式的环境吧!不足的地方请大家多交流.谢谢大家的支持 准备环境: 1, ubuntu系统,(我在16.04测试通过.其他版本请自行测试, ...
- sourcetree注册
http://www.cnblogs.com/xiofee/p/sourcetree_pass_initialization_setup.html
- Spring 的循环依赖,源码详细分析 → 真的非要三级缓存吗
开心一刻 吃完晚饭,坐在院子里和父亲聊天 父亲:你有什么人生追求? 我:金钱和美女 父亲对着我的头就是一丁弓,说道:小小年纪,怎么这么庸俗,重说一次 我:事业与爱情 父亲赞赏的摸了我的头,说道:嗯嗯, ...
- 【论文研读】Sabir, Ekraam, et al. "Recurrent convolutional strategies for face manipulation detection in videos." Interfaces (GUI) 3.1 (2019).
#摘要 错误信息通过合成逼真的图像和视频进行传播这一严重问题,需要鲁棒的篡改检测方法来应对.尽管在检测静止图像上的面部篡改方面已付出了巨大的努力,但人们对于通过利用视频流中存在的时序信息,对视频中被篡 ...
- 阅读笔记:Very Deep Convolutional Networks for Large-Scale Image Recognition
摘要: 在这篇论文我们主要研究卷积神级网络的深度对大范围图像识别效果的影响,我们发现增加神经网络层数增加到16-19层时我们的实验结果有很大的提高.这使得我们在2014年的ImageNet Chall ...