鑳屾櫙
浼楁墍鍛ㄧ煡锛屾棭鏈� JavaScript 鍘熺敓骞朵笉鏀寔妯″潡鍖栵紝鐩村埌 2015 骞达紝TC39 鍙戝竷 ES6锛屽叾涓湁涓�涓鑼冨氨鏄� ES modules
锛堜负浜嗘柟渚胯〃杩帮紝鍚庨潰缁熶竴绠�绉� ESM锛夈�備絾鏄湪 ES6 瑙勮寖鎻愬嚭鍓嶏紝灏卞凡缁忓瓨鍦ㄤ簡涓�浜涙ā鍧楀寲鏂规锛屾瘮濡� CommonJS锛坕n Node.js锛夈�丄MD銆侲SM 涓庤繖浜涜鑼冪殑鍏卞悓鐐瑰氨鏄兘鏀寔瀵煎叆锛坕mport锛夊拰瀵煎嚭锛坋xport锛夎娉曪紝鍙槸鍏惰涓虹殑鍏抽敭璇嶄篃涓�浜涘樊寮傘��
CommonJS
// add.js
const add = (a, b) => a + b
module.exports = add
// index.js
const add = require('./add')
add(1, 5)
AMD
// add.js
define(function() {
const add = (a, b) => a + b
return add
})
// index.js
require(['./add'], function (add) {
add(1, 5)
})
ESM
// add.js
const add = (a, b) => a + b
export default add
//index.js
import add from './add'
add(1, 5)
鍏充簬 JavaScript 妯″潡鍖栧嚭鐜扮殑鑳屾櫙鍦ㄤ笂涓�绔狅紙銆婂墠绔ā鍧楀寲鐨勫墠涓栥��)锛夊凡缁忔湁鎵�浠嬬粛锛岃繖閲屼笉鍐嶈禈杩般�備絾鏄� ESM 鐨勫嚭鐜颁笉鍚屼簬鍏朵粬鐨勮鑼冿紝鍥犱负杩欐槸 JavaScript 瀹樻柟鎺ㄥ嚭鐨勬ā鍧楀寲鏂规锛岀浉姣斾簬 CommonJS 鍜� AMD 鏂规锛孍SM閲囩敤浜嗗畬鍏ㄩ潤鎬佸寲鐨勬柟寮忚繘琛屾ā鍧楃殑鍔犺浇銆�
ESM瑙勮寖
妯″潡瀵煎嚭
妯″潡瀵煎嚭鍙湁涓�涓叧閿瘝锛�export
锛屾渶绠�鍗曠殑鏂规硶灏辨槸鍦ㄥ0鏄庣殑鍙橀噺鍓嶉潰鐩存帴鍔犱笂 export 鍏抽敭璇嶃��
export const name = 'Shenfq'
鍙互鍦� const銆乴et銆乿ar 鍓嶇洿鎺ュ姞涓� export锛屼篃鍙互鍦� function 鎴栬�� class 鍓嶉潰鐩存帴鍔犱笂 export銆�
export function getName() {
return name
}
export class Logger {
log(...args) {
console.log(...args)
}
}
涓婇潰鐨勫鍑烘柟娉曚篃鍙互浣跨敤澶ф嫭鍙风殑鏂瑰紡杩涜绠�鍐欍��
const name = 'Shenfq'
function getName() {
return name
}
class Logger {
log(...args) {
console.log(...args)
}
}
export { name, getName, Logger }
鏈�鍚庝竴绉嶈娉曪紝涔熸槸鎴戜滑缁忓父浣跨敤鐨勶紝瀵煎嚭榛樿妯″潡銆�
const name = 'Shenfq'
export default name
妯″潡瀵煎叆
妯″潡鐨勫鍏ヤ娇鐢�import
锛屽苟閰嶅悎 from
鍏抽敭璇嶃��
// main.js
import name from './module.js'
// module.js
const name = 'Shenfq'
export default name
杩欐牱鐩存帴瀵煎叆鐨勬柟寮忥紝module.js
涓繀椤讳娇鐢� export default
锛屼篃灏辨槸璇� import 璇硶锛岄粯璁ゅ鍏ョ殑鏄�default
妯″潡銆傚鏋滄兂瑕佸鍏ュ叾浠栨ā鍧楋紝灏卞繀椤讳娇鐢ㄥ璞″睍寮�鐨勮娉曘��
// main.js
import { name, getName } from './module.js'
// module.js
export const name = 'Shenfq'
export const getName = () => name
濡傛灉妯″潡鏂囦欢鍚屾椂瀵煎嚭浜嗛粯璁ゆā鍧楋紝鍜屽叾浠栨ā鍧楋紝鍦ㄥ鍏ユ椂锛屼篃鍙互鍚屾椂灏嗕袱鑰呭鍏ャ��
// main.js
import name, { getName } from './module.js'
//module.js
const name = 'Shenfq'
export const getName = () => name
export default name
褰撶劧锛孍SM 涔熸彁渚涗簡閲嶅懡鍚嶇殑璇硶锛屽皢瀵煎叆鐨勬ā鍧楄繘琛岄噸鏂板懡鍚嶃��
// main.js
import * as mod from './module.js'
let name = ''
name = mod.name
name = mod.getName()
// module.js
export const name = 'Shenfq'
export const getName = () => name
涓婅堪鍐欐硶灏辩浉褰撲簬浜庡皢妯″潡瀵煎嚭鐨勫璞¤繘琛岄噸鏂拌祴鍊硷細
// main.js
import { name, getName } from './module.js'
const mod = { name, getName }
鍚屾椂涔熷彲浠ュ鍗曠嫭鐨勫彉閲忚繘琛岄噸鍛藉悕锛�
// main.js
import { name, getName as getModName }
瀵煎叆鍚屾椂杩涜瀵煎嚭
濡傛灉鏈変袱涓ā鍧� a 鍜� b 锛屽悓鏃跺紩鍏ヤ簡妯″潡 c锛屼絾鏄繖涓や釜妯″潡杩橀渶瑕佸鍏ユā鍧� d锛屽鏋滄ā鍧� a銆乥 鍦ㄥ鍏� c 涔嬪悗锛屽啀瀵煎叆 d 涔熸槸鍙互鐨勶紝浣嗘槸鏈変簺绻佺悙锛屾垜浠彲浠ョ洿鎺ュ湪妯″潡 c 閲岄潰瀵煎叆妯″潡 d锛屽啀鎶婃ā鍧� d 鏆撮湶鍑哄幓銆�
// module_c.js
import { name, getName } from './module_d.js'
export { name, getName }
杩欎箞鍐欑湅璧锋潵杩樻槸鏈変簺楹荤儲锛岃繖閲� ESM 鎻愪緵浜嗕竴绉嶅皢 import 鍜� export 杩涜缁撳悎鐨勮娉曘��
export { name, getName } from './module_d.js'
涓婇潰鏄� ESM 瑙勮寖鐨勪竴浜涘熀鏈娉曪紝濡傛灉鎯充簡瑙f洿澶氾紝鍙互缈婚槄闃�佸笀鐨� 銆奅S6 鍏ラ棬銆�銆�
ESM 涓� CommonJS 鐨勫樊寮�
棣栧厛鑲畾鏄娉曚笂鐨勫樊寮傦紝鍓嶉潰涔熷凡缁忕畝鍗曚粙缁嶈繃浜嗭紝涓�涓娇鐢� import/export
璇硶锛屼竴涓娇鐢� require/module
璇硶銆�
鍙︿竴涓� ESM 涓� CommonJS 鏄捐憲鐨勫樊寮傚湪浜庯紝ESM 瀵煎叆妯″潡鐨勫彉閲忛兘鏄己缁戝畾锛屽鍑烘ā鍧楃殑鍙橀噺涓�鏃﹀彂鐢熷彉鍖栵紝瀵瑰簲瀵煎叆妯″潡鐨勫彉閲忎篃浼氳窡闅忓彉鍖栵紝鑰� CommonJS 涓鍏ョ殑妯″潡閮芥槸鍊间紶閫掍笌寮曠敤浼犻�掞紝绫讳技浜庡嚱鏁颁紶鍙傦紙鍩烘湰绫诲瀷杩涜鍊间紶閫掞紝鐩稿綋浜庢嫹璐濆彉閲忥紝闈炲熀纭�绫诲瀷銆愬璞°�佹暟缁勩�戯紝杩涜寮曠敤浼犻�掞級銆�
涓嬮潰鎴戜滑鐪嬩笅璇︾粏鐨勬渚嬶細
CommonJS
// a.js
const mod = require('./b')
setTimeout(() => {
console.log(mod)
}, 1000)
// b.js
let mod = 'first value'
setTimeout(() => {
mod = 'second value'
}, 500)
module.exports = mod
$ node a.js
first value
ESM
// a.mjs
import { mod } from './b.mjs'
setTimeout(() => {
console.log(mod)
}, 1000)
// b.mjs
export let mod = 'first value'
setTimeout(() => {
mod = 'second value'
}, 500)
$ node --experimental-modules a.mjs
# (node:99615) ExperimentalWarning: The ESM module loader is experimental.
second value
鍙﹀锛孋ommonJS 鐨勬ā鍧楀疄鐜帮紝瀹為檯鏄粰姣忎釜妯″潡鏂囦欢鍋氫簡涓�灞傚嚱鏁板寘瑁癸紝浠庤�屼娇寰楁瘡涓ā鍧楄幏鍙� require/module
銆�__filename/__dirname
鍙橀噺銆傞偅涓婇潰鐨� a.js
鏉ヤ妇渚嬶紝瀹為檯鎵ц杩囩▼涓� a.js
杩愯浠g爜濡備笅锛�
// a.js
(function(exports, require, module, __filename, __dirname) {
const mod = require('./b')
setTimeout(() => {
console.log(mod)
}, 1000)
});
鑰� ESM 鐨勬ā鍧楁槸閫氳繃 import/export
鍏抽敭璇嶆潵瀹炵幇锛屾病鏈夊搴旂殑鍑芥暟鍖呰9锛屾墍浠ュ湪 ESM 妯″潡涓紝闇�瑕佷娇鐢� import.meta
鍙橀噺鏉ヨ幏鍙� __filename/__dirname
銆�import.meta
鏄� ECMAScript 瀹炵幇鐨勪竴涓寘鍚ā鍧楀厓鏁版嵁鐨勭壒瀹氬璞★紝涓昏鐢ㄤ簬瀛樻斁妯″潡鐨� url
锛岃�� node 涓彧鏀寔鍔犺浇鏈湴妯″潡锛屾墍浠� url 閮芥槸浣跨敤 file:
鍗忚銆�
import url from 'url'
import path from 'path'
// import.meta: { url: file:///Users/dev/mjs/a.mjs }
const __filename = url.fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
鍔犺浇鐨勫師鐞�
姝ラ锛�
- Construction锛堟瀯閫狅級锛氫笅杞芥墍鏈夌殑鏂囦欢骞朵笖瑙f瀽涓簃odule records銆�
- Instantiation锛堝疄渚嬶級锛氭妸鎵�鏈夊鍑虹殑鍙橀噺鍏ュ唴瀛樻寚瀹氫綅缃紙浣嗘槸鏆傛椂杩樹笉姹傚�硷級銆傜劧鍚庯紝璁╁鍑哄拰瀵煎叆閮芥寚鍚戝唴瀛樻寚瀹氫綅缃�傝繖鍙仛銆巐inking(閾炬帴)銆忋��
- Evaluation锛堟眰鍊硷級锛氭墽琛屼唬鐮侊紝寰楀埌鍙橀噺鐨勫�肩劧鍚庢斁鍒板唴瀛樺搴斾綅缃��
妯″潡璁板綍
鎵�鏈夌殑妯″潡鍖栧紑鍙戯紝閮芥槸浠庝竴涓叆鍙f枃浠跺紑濮嬶紝鏃犺鏄� Node.js 杩樻槸娴忚鍣紝閮戒細鏍规嵁杩欎釜鍏ュ彛鏂囦欢杩涜妫�绱紝涓�姝ヤ竴姝ユ壘鍒板叾浠栨墍鏈夌殑渚濊禆鏂囦欢銆�
// Node.js: main.mjs
import Log from './log.mjs'
鍊煎緱娉ㄦ剰鐨勬槸锛屽垰寮�濮嬫嬁鍒板叆鍙f枃浠讹紝鎴戜滑骞朵笉鐭ラ亾瀹冧緷璧栦簡鍝簺妯″潡锛屾墍浠ュ繀椤诲厛閫氳繃 js 寮曟搸闈欐�佸垎鏋愶紝寰楀埌涓�涓ā鍧楄褰曪紝璇ヨ褰曞寘鍚簡璇ユ枃浠剁殑渚濊禆椤广�傛墍浠ワ紝涓�寮�濮嬫嬁鍒扮殑 js 鏂囦欢骞朵笉浼氭墽琛岋紝鍙槸浼氬皢鏂囦欢杞崲寰楀埌涓�涓ā鍧楄褰曪紙module records锛夈�傛墍鏈夌殑 import 妯″潡閮藉湪妯″潡璁板綍鐨� importEntries
瀛楁涓褰曪紝鏇村妯″潡璁板綍鐩稿叧鐨勫瓧娈靛彲浠ユ煡闃�tc39.es銆�
妯″潡鏋勯��
寰楀埌妯″潡璁板綍鍚庯紝浼氫笅杞芥墍鏈変緷璧栵紝骞跺啀娆″皢渚濊禆鏂囦欢杞崲涓烘ā鍧楄褰曪紝涓�鐩存寔缁埌娌℃湁渚濊禆鏂囦欢涓烘锛岃繖涓繃绋嬭绉颁负銆庢瀯閫犮�忥紙construction锛夈��
妯″潡鏋勯�犲寘鎷涓嬩笁涓楠わ細
- 妯″潡璇嗗埆锛堣В鏋愪緷璧栨ā鍧� url锛屾壘鍒扮湡瀹炵殑涓嬭浇璺緞锛夛紱
- 鏂囦欢涓嬭浇锛堜粠鎸囧畾鐨� url 杩涜涓嬭浇锛屾垨浠庢枃浠剁郴缁熻繘琛屽姞杞斤級锛�
- 杞寲涓烘ā鍧楄褰曪紙module records锛夈��
瀵逛簬濡備綍灏嗘ā鍧楁枃浠惰浆鍖栦负妯″潡璁板綍锛孍SM 瑙勮寖鏈夎缁嗙殑璇存槑锛屼絾鏄湪鏋勯�犺繖涓楠や腑锛岃鎬庝箞涓嬭浇寰楀埌杩欎簺渚濊禆鐨勬ā鍧楁枃浠讹紝鍦� ESM 瑙勮寖涓苟娌℃湁瀵瑰簲鐨勮鏄庛�傚洜涓哄浣曚笅杞芥枃浠讹紝鍦ㄦ湇鍔$鍜屽鎴风閮芥湁涓嶅悓鐨勫疄鐜拌鑼冦�傛瘮濡傦紝鍦ㄦ祻瑙堝櫒涓紝濡備綍涓嬭浇鏂囦欢鏄睘浜� HTML 瑙勮寖锛堟祻瑙堝櫒鐨勬ā鍧楀姞杞介兘鏄娇鐢ㄧ殑 鏍囩锛夈��
铏界劧涓嬭浇瀹屽叏涓嶅睘浜� ESM 鐨勭幇鏈夎鑼冿紝浣嗗湪 import
璇彞涓繕鏈変竴涓紩鐢ㄦā鍧楃殑 url 鍦板潃锛屽叧浜庤繖涓湴鍧�闇�瑕佸浣曡浆鍖栵紝鍦� Node 鍜屾祻瑙堝櫒涔嬮棿鏈変細鍑虹幇涓�浜涘樊寮傘�傜畝鍗曟潵璇达紝鍦� Node 涓彲浠ョ洿鎺� import 鍦� node_modules 涓殑妯″潡锛岃�屽湪娴忚鍣ㄤ腑骞朵笉鑳界洿鎺ヨ繖涔堝仛锛屽洜涓烘祻瑙堝櫒鏃犳硶姝g‘鐨勬壘鍒版湇鍔″櫒涓婄殑 node_modules 鐩綍鍦ㄥ摢閲屻�傚ソ鍦ㄦ湁涓�涓彨鍋� import-maps 鐨勬彁妗堬紝璇ユ彁妗堜富瑕佸氨鏄敤鏉ヨВ鍐虫祻瑙堝櫒鏃犳硶鐩存帴瀵煎叆妯″潡鏍囪瘑绗︾殑闂銆備絾鏄紝鍦ㄨ鎻愭鏈瀹屽叏瀹炵幇涔嬪墠锛屾祻瑙堝櫒涓緷鐒跺彧鑳戒娇鐢� url 杩涜妯″潡瀵煎叆銆�
涓嬭浇濂界殑妯″潡锛岄兘浼氳杞寲涓烘ā鍧楄褰曠劧鍚庣紦瀛樺埌 module map
涓紝閬囧埌涓嶅悓鏂囦欢鑾峰彇鐨勭浉鍚屼緷璧栵紝閮戒細鐩存帴鍦� module map
缂撳瓨涓幏鍙栥��
// log.js
const log = console.log
export default log
// file.js
export {
readFileSync as read,
writeFileSync as write
} from 'fs'
妯″潡瀹炰緥
鑾峰彇鍒版墍鏈変緷璧栨枃浠跺苟寤虹珛濂� module map
鍚庯紝灏变細鎵惧埌鎵�鏈夋ā鍧楄褰曪紝骞跺彇鍑哄叾涓殑鎵�鏈夊鍑虹殑鍙橀噺锛岀劧鍚庯紝灏嗘墍鏈夊彉閲忎竴涓�瀵瑰簲鍒板唴瀛樹腑锛屽皢瀵瑰簲鍏崇郴瀛樺偍鍒般�庢ā鍧楃幆澧冭褰曘�忥紙module environment record锛変腑銆傚綋鐒跺綋鍓嶅唴瀛樹腑鐨勫彉閲忓苟娌℃湁鍊硷紝鍙槸鍒濆鍖栦簡瀵瑰簲鍏崇郴銆傚垵濮嬪寲瀵煎嚭鍙橀噺鍜屽唴瀛樼殑瀵瑰簲鍏崇郴鍚庯紝绱ф帴鐫�浼氳缃ā鍧楀鍏ュ拰鍐呭瓨鐨勫搴斿叧绯伙紝纭繚鐩稿悓鍙橀噺鐨勫鍏ュ拰瀵煎嚭閮芥寚鍚戜簡鍚屼竴涓唴瀛樺尯鍩燂紝骞朵繚璇佹墍鏈夌殑瀵煎叆閮借兘鎵惧埌瀵瑰簲鐨勫鍑恒��
鐢变簬瀵煎叆鍜屽鍑烘寚鍚戝悓涓�鍐呭瓨鍖哄煙锛屾墍浠ュ鍑哄�间竴鏃﹀彂鐢熷彉鍖栵紝瀵煎叆鍊间篃浼氬彉鍖栵紝涓嶅悓浜� CommonJS锛孋ommonJS 鐨勬墍鏈夊�奸兘鏄熀浜庢嫹璐濈殑銆傝繛鎺ュ埌瀵煎叆瀵煎嚭鍙橀噺鍚庯紝鎴戜滑灏遍渶瑕佸皢瀵瑰簲鐨勫�兼斁鍏ュ埌鍐呭瓨涓紝涓嬮潰灏辫杩涘叆鍒版眰鍊肩殑姝ラ浜嗐��
妯″潡姹傚��
姹傚�兼楠ょ浉瀵圭畝鍗曪紝鍙杩愯浠g爜鎶婅绠楀嚭鏉ョ殑鍊煎~鍏ヤ箣鍓嶈褰曠殑鍐呭瓨鍦板潃灏卞彲浠ヤ簡銆傚埌杩欓噷灏卞凡缁忚兘澶熸剦蹇殑浣跨敤 ESM 妯″潡鍖栦簡銆�
ESM鐨勮繘灞�
鍥犱负 ESM 鍑虹幇杈冩櫄锛屾湇鍔$宸叉湁 CommonJS 鏂规锛屽鎴风鍙堟湁 webpack 鎵撳寘宸ュ叿锛屾墍浠� ESM 鐨勬帹骞夸笉寰椾笉璇磋繕鏄崄鍒嗚壈闅剧殑銆�
瀹㈡埛绔�
鎴戜滑鍏堢湅鐪嬪鎴风鐨勬敮鎸佹儏鍐碉紝杩欓噷鎺ㄨ崘澶у鍒� Can I Use 鐩存帴鏌ョ湅锛屼笅鍥炬槸 2019/11
鐨勬埅鍥俱��
鐩墠涓烘锛屼富娴佹祻瑙堝櫒閮藉凡缁忔敮鎸� ESM 浜嗭紝鍙渶鍦� script
鏍囩浼犲叆鎸囧畾鐨� type="module"
鍗冲彲銆�
鍙﹀锛屾垜浠煡閬撳湪 Node.js 涓紝瑕佷娇鐢� ESM 鏈夋椂鍊欓渶瑕佺敤鍒� .mjs 鍚庣紑锛屼絾鏄祻瑙堝櫒骞朵笉鍏冲績鏂囦欢鍚庣紑锛屽彧闇�瑕� http 鍝嶅簲澶寸殑MIME绫诲瀷姝g‘鍗冲彲锛�Content-Type: text/javascript
锛夈�傚悓鏃讹紝褰� type="module"
鏃讹紝榛樿鍚敤 defer
鏉ュ姞杞借剼鏈�傝繖閲岃ˉ鍏呬竴寮� defer銆乤sync 宸紓鍥俱��
鎴戜滑鐭ラ亾娴忚鍣ㄤ笉鏀寔 script
鐨勬椂鍊欙紝鎻愪緵浜� noscript
鏍囩鐢ㄤ簬闄嶇骇澶勭悊锛屾ā鍧楀寲涔熸彁渚涗簡绫讳技鐨勬爣绛俱��
杩欐牱鎴戜滑灏辫兘閽堝鏀寔 ESM 鐨勬祻瑙堝櫒鐩存帴浣跨敤妯″潡鍖栨柟妗堝姞杞芥枃浠讹紝涓嶆敮鎸佺殑娴忚鍣ㄨ繕鏄娇鐢� webpack 鎵撳寘鐨勭増鏈��
棰勫姞杞�
鎴戜滑鐭ラ亾娴忚鍣ㄧ殑 link 鏍囩鍙互鐢ㄤ綔璧勬簮鐨勯鍔犺浇锛屾瘮濡傛垜闇�瑕侀鍏堝姞杞� main.js
鏂囦欢锛�
濡傛灉杩欎釜 main.js
鏂囦欢鏄竴涓ā鍧楀寲鏂囦欢锛屾祻瑙堝櫒浠呬粎棰勫厛鍔犺浇鍗曠嫭杩欎竴涓枃浠舵槸娌℃湁鎰忎箟鐨勶紝鍓嶉潰鎴戜滑涔熻杩囷紝涓�涓ā鍧楀寲鏂囦欢涓嬭浇鍚庤繕闇�瑕佽浆鍖栧緱鍒版ā鍧楄褰曪紝杩涜妯″潡瀹炰緥銆佹ā鍧楁眰鍊艰繖浜涙搷浣滐紝鎵�浠ユ垜浠緱鎯冲姙娉曞憡璇夋祻瑙堝櫒锛岃繖涓枃浠舵槸涓�涓ā鍧楀寲鐨勬枃浠讹紝鎵�浠ユ祻瑙堝櫒鎻愪緵浜嗕竴绉嶆柊鐨� rel 绫诲瀷锛屼笓闂ㄧ敤浜庢ā鍧楀寲鏂囦欢鐨勯鍔犺浇銆�
鐜扮姸
铏界劧涓绘祦娴忚鍣ㄩ兘宸茬粡鏀寔浜� ESM锛屼絾鏄牴鎹� chrome 鐨勭粺璁�锛屾湁鐢ㄥ埌 鐨勯〉闈㈠彧鏈� 1%銆傛埅鍥炬椂闂翠负
2019/11
銆�
鏈嶅姟绔�
娴忚鍣ㄨ兘澶熼�氳繃 script 鏍囩鎸囧畾褰撳墠鑴氭湰鏄惁浣滀负妯″潡澶勭悊锛屼絾鏄湪 Node.js 涓病鏈夊緢鏄庣‘鐨勬柟寮忔潵琛ㄧず鏄惁闇�瑕佷娇鐢� ESM锛岃�屼笖 Node.js 涓湰韬氨宸茬粡鏈変簡 CommonJS 鐨勬爣鍑嗘ā鍧楀寲鏂规銆傚氨绠楀紑鍚簡 ESM锛屽張閫氳繃浣曠鏂瑰紡鏉ュ垽鏂綋鍓嶅叆鍙f枃浠跺鍏ョ殑妯″潡鍒板簳鏄娇鐢ㄧ殑 ESM 杩樻槸 CommonJS 鍛紵涓轰簡瑙e喅涓婅堪闂锛宯ode 绀惧尯寮�濮嬪嚭鐜颁簡 ESM 鐨勭浉鍏宠崏妗堬紝鍏蜂綋鍙互鍦� github 涓婃煡闃呫��
2017骞村彂甯冪殑 Node.js 8.5.0 寮�鍚簡 ESM 鐨勫疄楠屾�ф敮鎸侊紝鍦ㄥ惎鍔ㄧ▼搴忔椂锛屽姞涓� --experimental-modules
鏉ュ紑鍚 ESM 鐨勬敮鎸侊紝骞跺皢 .mjs
鍚庣紑鐨勬枃浠跺綋鍋� ESM 鏉ヨВ鏋愩�傛棭鏈熺殑鏈熸湜鏄湪 Node.js 12 杈惧埌 LTS 鐘舵�佹寮忓彂甯冿紝鐒跺悗鏈熸湜骞舵病鏈夊疄鐜帮紝鐩村埌鏈�杩戠殑 13.2.0 鐗堟湰鎵嶆寮忔敮鎸� ESM锛屼篃灏辨槸鍙栨秷浜� --experimental-modules
鍚姩鍙傛暟銆傚叿浣撶粏鑺傚彲浠ユ煡鐪� Node.js 13.2.0 鐨�瀹樻柟鏂囨。銆�
鍏充簬 .mjs
鍚庣紑绀惧尯鏈変袱绉嶅畬鍏ㄤ笉鍚岀殑鎬佸害銆傛敮鎸佺殑涓�鏂硅涓洪�氳繃鏂囦欢鍚庣紑鍖哄垎绫诲瀷鏄渶绠�鍗曚篃鏄渶鏄庣‘鐨勬柟寮忥紝涓旂ぞ鍖烘棭宸叉湁绫讳技妗堜緥锛屼緥濡傦紝.jsx
鐢ㄤ簬 React 缁勪欢銆�.ts
鐢ㄤ簬 ts 鏂囦欢锛涜�屾敮鎸佺殑涓�鏂硅涓猴紝.js
浣滀负 js 鍚庣紑宸茬粡瀛樺湪杩欎箞澶氬勾锛岃瑙変笂寰堥毦鎺ュ彈涓�涓� .mjs
涔熸槸 js 鏂囦欢锛岃�屼笖鐜版湁鐨勫緢澶氬伐鍏烽兘鏄互 .js
鍚庣紑鏉ヨ瘑鍒� js 鏂囦欢锛屽鏋滃紩鍏ヤ簡 .mjs
鏂规锛屽氨鏈夊ぇ鎵归噺鐨勫伐鍏烽渶瑕佷慨鏀规潵鏈夋晥鐨勯�傞厤 ESM銆�
鎵�浠ラ櫎浜� .mjs
鍚庣紑鎸囧畾 ESM 澶栵紝杩樺彲浠ヤ娇鐢� pkg.json
鏂囦欢鐨� type
灞炴�с�傚鏋� type 灞炴�т负 module锛屽垯琛ㄧず褰撳墠妯″潡搴斾娇鐢� ESM 鏉ヨВ鏋愭ā鍧楋紝鍚﹀垯浣跨敤 CommonJS 瑙f瀽妯″潡銆�
{
"type": "module" // module | commonjs(default)
}
褰撶劧鏈変簺鏈湴鏂囦欢鏄病鏈� pkg.json
鐨勶紝浣嗘槸浣犲張涓嶆兂浣跨敤 .mjs
鍚庣紑锛岃繖鏃跺�欏彧闇�瑕佸湪鍛戒护琛屽姞涓婁竴涓惎鍔ㄥ弬鏁� --input-type=module
銆傚悓鏃� input-type
涔熸敮鎸� commonjs 鍙傛暟鏉ユ寚瀹氫娇鐢� CommonJS锛�-鈥攊nput-type=commonjs
锛夈��
鎬荤粨涓�涓嬶紝Node.js 涓紝浠ヤ笅涓夌鎯呭喌浼氬惎鐢� ESM 鐨勬ā鍧楀姞杞芥柟寮忥細
- 鏂囦欢鍚庣紑涓�
.mjs
; -
pkg.json
涓� type 瀛楁鎸囧畾涓�module
锛� - 鍚姩鍙傛暟娣诲姞
--input-type=module
銆�
鍚屾牱锛屼篃鏈変笁绉嶆儏鍐典細鍚敤 CommonJS 鐨勬ā鍧楀姞杞芥柟寮忥細
- 鏂囦欢鍚庣紑涓�
.cjs
; -
pkg.json
涓� type 瀛楁鎸囧畾涓�commonjs
锛� - 鍚姩鍙傛暟娣诲姞
--input-type=commonjs
銆�
铏界劧 13.2 鐗堟湰鍘婚櫎浜� --experimental-modules
鐨勫惎鍔ㄥ弬鏁帮紝浣嗘槸鎸夌収鏂囨。鐨勮娉曪紝鍦� Node.js 涓娇鐢� ESM 渚濇棫鏄疄楠岀壒鎬с��
Stability: 1 - Experimental
涓嶈繃锛岀浉淇$瓑鍒� Node.js 14 LTS 鐗堟湰鍙戝竷鏃讹紝ESM 鐨勬敮鎸佸簲璇ュ氨鑳借繘鍏ョǔ瀹氶樁娈典簡锛岃繖閲岃繕鏈変竴涓� Node.js 鍏充簬 ESM 鐨勬暣涓�璁″垝鍒楄〃鍙互鏌ラ槄銆�
鍙傝��
- nodejs/modules
- Module specifiers: what鈥檚 new with ES modules?
- 鍥捐 ES Modules锛�ES modules: A cartoon deep-dive锛�
鏇村绮惧僵璇风偣鍑� 馃憠馃憠馃憠 闃呰鍘熸枃 馃槈