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

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

  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] 05 - Modules and Function

    一个 Node.js 文件就是一个模块,这个文件可能是JavaScript 代码.JSON 或者编译过的C/C++ 扩展. 模块是Node.js 应用程序的基本组成部分,文件和模块是一一对应的. No ...

  4. [Node.js] Exporting Modules in Node

    In this lesson, you will learn the difference between the exports statement and module.exports. Two ...

  5. [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 ...

  6. [Node.js] CommonJS Modules

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

  7. Node.js学习 - Modules

    创建模块 当前目录:hello.js, main.js // hello.js exports.world = function() { // exports 对象把 world 作为模块的访问接口 ...

  8. Node.js require 模块加载原理 All In One

    Node.js require 模块加载原理 All In One require 加载模块,搜索路径 "use strict"; /** * * @author xgqfrms ...

  9. 【nodejs笔记1】配置webstorm + node.js +express + mongodb开发博客的环境

    1. 安装webstorm 并破解 2. 安装node (以及express框架) 至官网下载并安装.(http://nodejs.org)v0.10.32   msi  安装后测试,打开命令行, c ...

随机推荐

  1. Markdown 编辑器+同步预览+文件笔记管理+静态博客 metadata 管理

    Leanote: 1. 笔记管理, 支持富文本, markdown, 写作模式.... 编辑器绝对好用. 另外特意为coder制作了一个贴代码的插件, 真是太贴心(因为作者也是coder) 2. 博客 ...

  2. 给定HDFS中某一个目录,输出该目录下的所有文件的读写权限、大小、创建时间、路径等信息,如果该文件是目录,则递归输出该目录下所有文件相关信息。

    1 import java.text.SimpleDateFormat; 2 import org.apache.hadoop.fs.*; 3 4 public class E_RecursiveRe ...

  3. moco框架实现重定向

    一.重定向到百度 1.代码 2.运行结果 因为没哟填写别的,浏览器输入路径: localhost:8888/redirect 点击回车,跳转到百度 二.跳转到自己的网站 1.代码 2.运行结果 输入准 ...

  4. Go 和 Syscall

    曹春晖:谈一谈 Go 和 Syscall https://juejin.im/post/6844903845475139597

  5. net.core.somaxconn net.ipv4.tcp_max_syn_backlog

    Linux参数-net.core.somaxconn与net.ipv4.tcp_max_syn_backlog_梁海江的博客-CSDN博客_net.ipv4.tcp_max_syn_backlog h ...

  6. Crypto.getRandomValues()

    Crypto.getRandomValues() - Web APIs | MDN https://developer.mozilla.org/en-US/docs/Web/API/Crypto/ge ...

  7. 实用 nginx.conf 用法大全

    服务器拒绝非GET方式请求保障安全性,因为 DELETE.POST.PUT 是可以修改数据的. Nginx 解决方案 在 nginx.conf 配置文件的网站配置区域中添加如下代码片段: 非 GET ...

  8. Docker Desktop启动Kubernetes

    Docker_Desktop启动Kubernetes 参考仓库:https://github.com/AliyunContainerService/k8s-for-docker-desktop 视频参 ...

  9. Phoenix简介概述,Phoenix的Java API 相关操作优秀案例

    Phoenix简介概述,Phoenix的Java API 相关操作优秀案例 一.Phoenix概述简介 二.Phoenix实例一:Java API操作 2.1 phoenix.properties 2 ...

  10. python自动化之使用allure生成测试报告

    Allure测试报告框架帮助你轻松实现"高大上"报告展示.本文通过示例演示如何从0到1集成Allure测试框架.重点展示了如何将Allure集成到已有的自动化测试工程中.以及如何实 ...