esm(ECMAScript Modules) 和 cjs(CommonJS) 沒有語法區(qū)別只有模塊導(dǎo)入導(dǎo)出的差異。
nodejs 創(chuàng)立 require ,瀏覽器是
考慮nodejs 瀏覽器等歷史因素,成因復(fù)雜,沒有必要較真,掌握用法就行。
esm 導(dǎo)出:
export const a = 0
export default 0
//export const default = 0
cjs 導(dǎo)出:
module.exports.a = 0
module.exports.default = 0
esm 導(dǎo)入:
import a, { b } from "m" //靜態(tài)導(dǎo)出
//import { default as a, b } from "m"
const {default:myM} =await import('m') //動(dòng)態(tài)載入
cjs 導(dǎo)入:
const { default: a, b } = require("m")
esm 導(dǎo)入整個(gè)模塊:
import * as m from "m"
cjs 導(dǎo)入整個(gè)模塊:
const m = require("m")