{"id":10638,"date":"2023-04-12T10:22:00","date_gmt":"2023-04-12T02:22:00","guid":{"rendered":"http:\/\/123.57.164.21\/?p=10638"},"modified":"2023-04-12T10:22:00","modified_gmt":"2023-04-12T02:22:00","slug":"nodejs%e6%a8%a1%e5%9d%97%e6%98%af%e4%bb%80%e4%b9%88","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=10638","title":{"rendered":"nodejs\u6a21\u5757\u662f\u4ec0\u4e48"},"content":{"rendered":"\n<p><strong>\u524d\u8a00<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/04\/image-126.png\" alt=\"\" class=\"wp-image-10639\" width=\"420\" height=\"233\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-126.png 724w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-126-300x167.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-126-230x128.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-126-350x194.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-126-480x267.png 480w\" sizes=\"(max-width: 420px) 100vw, 420px\" \/><\/figure><\/div>\n\n\n\n<p>\u6a21\u5757\u662fNode.js\u5e94\u7528\u7a0b\u5e8f\u7684\u57fa\u672c\u7ec4\u6210\u90e8\u5206\uff0c\u6587\u4ef6\u548c\u6a21\u5757\u662f\u4e00\u4e00\u5bf9\u5e94\u7684\uff0c\u4e00\u4e2aNodejs\u6a21\u5757\u5c31\u662f\u4e00\u4e2a\u6587\u4ef6\uff0c\u800c\u8fd9\u4e2a\u6587\u4ef6\u53ef\u80fd\u662fJavaScript\u4ee3\u7801\u3001JSON\u6216\u8005\u7f16\u8bd1\u8fc7\u7684\u201cC\/C++\u201d\u6269\u5c55\uff0c\u5f15\u7528\u6a21\u5757\u53ef\u7528\u201crequire(&#8216;\u6587\u4ef6\u8def\u5f84&#8217;)\u201d\u8bed\u53e5\u3002<\/p>\n\n\n\n<p>\u5bf9\u4e8enodejs\u6765\u8bf4\uff0c\u4e00\u4e2a\u6587\u4ef6\u5c31\u662f\u4e00\u4e2a\u6a21\u5757\uff0c\u4f60\u53ef\u4ee5export\u63a5\u53e3\u51fa\u53bb\uff0c\u4e5f\u53ef\u4ee5require\u522b\u7684\u6a21\u5757\u8fdb\u6765\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    \/\/ module1.js\n    exports.func1 = function(){\n            console.log('func1 from module1 called');\n    }<\/pre>\n\n\n\n<p>module1\u628a\u51fd\u6570func1\u901a\u8fc7exports\u5bf9\u8c61\u4f5c\u4e3a\u6a21\u5757\u7684\u516c\u5171\u8bbf\u95ee\u63a5\u53e3\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    \/\/module2.js\n    var in_module1 = require('.\/module1.js');\n    in_module1.func1();\n    exports.func2 = function(){\n            console.log('func2 from module2 called');\n    }<\/pre>\n\n\n\n<p>module2\u628amodule1 require\u8fdb\u6765\uff0c\u8fd9\u4e2a\u65f6\u5019\uff0cin_module1\u5c31\u76f8\u5f53\u4e8emodule1\u7684exports\u5bf9\u8c61\u3002\u5f53\u4f7f\u7528in_module1\u8c03\u7528func1\u7684\u65f6\u5019\uff0c\u76f8\u5f53\u4e8e\u901a\u8fc7module1\u7684exports\u5bf9\u8c61\u8c03\u7528func1\u3002<\/p>\n\n\n\n<p>\u540c\u65f6\uff0cmodule2\u81ea\u5df1\u7684\u51fd\u6570func2\u4e5f\u901a\u8fc7\u6a21\u5757\u7684exports\u5bf9\u8c61\u4f5c\u4e3amodule2\u516c\u5171\u63a5\u53e3\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    \/\/ module3.js\n    var in_module2 = require('.\/module2.js');\n    in_module2.func2();<\/pre>\n\n\n\n<p>\u540c\u7406\uff0cmodule3\u628amodule2 require\u8fdb\u6765\uff0c\u6b64\u65f6in_module2\u5c31\u76f8\u5f53\u4e8emodule2\u7684exports\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    rlan@rlan-LA:~\/nodejs\/nodetest$ node module2.js\n    func1 from module1 called\n    rlan@rlan-LA:~\/nodejs\/nodetest$ node module3.js\n    func1 from module1 called\n    func2 from module2 called<\/pre>\n\n\n\n<p><strong>NodeJS\u6a21\u5757\u7cfb\u7edf<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>NodeJS\u76ee\u524d\u6709\u4e24\u4e2a\u7cfb\u7edf\uff1a\u4e00\u5957\u662fCommonJS(\u7b80\u79f0CJS)\uff0c\u53e6\u4e00\u5957\u662fECMAScript modules(\u7b80\u79f0ESM); \u672c\u7bc7\u5185\u5bb9\u4e3b\u8981\u4e09\u4e2a\u8bdd\u9898\uff1a<\/p>\n\n\n\n<ul><li>1. CommonJS\u7684\u5185\u90e8\u539f\u7406<\/li><li>2. NodeJS\u5e73\u53f0\u7684ESM\u6a21\u5757\u7cfb\u7edf<\/li><li>3. CommonJS\u4e0eESM\u7684\u533a\u522b\uff1b\u5982\u4f55\u5728\u4e24\u5957\u7cfb\u7edf\u8fdb\u884c\u8f6c\u6362<\/li><\/ul>\n\n\n\n<p>\u9996\u5148\u8bb2\u8bb2\u4e3a\u4ec0\u4e48\u8981\u6709\u6a21\u5757\u7cfb\u7edf<\/p>\n\n\n\n<p><strong>\u4e3a\u4ec0\u4e48\u8981\u6709\u6a21\u5757\u7cfb\u7edf<\/strong><\/p>\n\n\n\n<p>\u4e00\u95e8\u597d\u7684\u8bed\u8a00\u4e00\u5b9a\u8981\u6709\u6a21\u5757\u7cfb\u7edf\uff0c\u56e0\u4e3a\u5b83\u80fd\u4e3a\u6211\u4eec\u89e3\u51b3\u5de5\u7a0b\u4e2d\u9047\u5230\u7684\u57fa\u672c\u9700\u6c42<\/p>\n\n\n\n<ul><li>\u628a\u529f\u80fd\u8fdb\u884c\u6a21\u5757\u62c6\u5206\uff0c\u80fd\u591f\u8ba9\u4ee3\u7801\u66f4\u5177\u6709\u6761\u7406\uff0c\u66f4\u5bb9\u6613\u7406\u89e3\uff0c\u80fd\u591f\u8ba9\u6211\u4eec\u5355\u72ec\u5f00\u53d1\u5e76\u6d4b\u8bd5\u5404\u4e2a\u5b50\u6a21\u5757\u7684\u529f\u80fd<\/li><li>\u80fd\u591f\u5bf9\u529f\u80fd\u8fdb\u884c\u5c01\u88c5\uff0c\u7136\u540e\u518d\u5176\u4ed6\u6a21\u5757\u80fd\u591f\u76f4\u63a5\u5f15\u5165\u4f7f\u7528\uff0c\u63d0\u9ad8\u590d\u7528\u6027<\/li><li>\u5b9e\u73b0\u5c01\u88c5\uff1a\u53ea\u9700\u8981\u5bf9\u5916\u63d0\u4f9b\u7b80\u5355\u7684\u8f93\u5165\u8f93\u51fa\u6587\u6863\uff0c\u5185\u90e8\u5b9e\u73b0\u80fd\u591f\u5bf9\u5916\u5c4f\u853d\uff0c\u51cf\u5c11\u7406\u89e3\u6210\u672c<\/li><li>\u7ba1\u7406\u4f9d\u8d56\u5173\u7cfb\uff1a\u597d\u7684\u6a21\u5757\u7cfb\u7edf\u80fd\u591f\u8ba9\u5f00\u53d1\u8005\u6839\u636e\u73b0\u6709\u7684\u7b2c\u4e09\u65b9\u6a21\u5757\uff0c\u8f7b\u677e\u7684\u6784\u5efa\u5176\u4ed6\u6a21\u5757\u3002\u53e6\u5916\u6a21\u5757\u7cfb\u7edf\u80fd\u591f\u8ba9\u7528\u6237\u7b80\u5355\u5f15\u5165\u81ea\u5df1\u60f3\u8981\u7684\u6a21\u5757\uff0c\u5e76\u4e14\u628a\u4f9d\u8d56\u94fe\u4e0a\u7684\u6a21\u5757\u8fdb\u884c\u5f15\u5165<\/li><\/ul>\n\n\n\n<p>\u521a\u5f00\u59cb\u7684\u65f6\u5019\uff0cJavaScript\u5e76\u6ca1\u6709\u597d\u7684\u6a21\u5757\u7cfb\u7edf\uff0c\u9875\u9762\u4e3b\u8981\u662f\u901a\u8fc7\u591a\u4e2ascript\u6807\u7b7e\u5f15\u5165\u4e0d\u540c\u7684\u8d44\u6e90\u3002\u4f46\u662f\u968f\u7740\u7cfb\u7edf\u7684\u9010\u6e10\u590d\u6742\u5316\uff0c\u4f20\u7edf\u7684script\u6807\u7b7e\u6a21\u5f0f\u4e0d\u80fd\u6ee1\u8db3\u4e1a\u52a1\u9700\u6c42\uff0c\u6240\u4ee5\u624d\u5f00\u59cb\u8ba1\u5212\u5b9a\u4e49\u4e00\u5957\u6a21\u5757\u7cfb\u7edf\uff0c\u6709AMD,UMD\u7b49\u7b49<\/p>\n\n\n\n<p>NodeJS\u662f\u8fd0\u884c\u5728\u540e\u53f0\u7684\u4e00\u95e8\u670d\u52a1\u7aef\u8bed\u8a00\uff0c\u76f8\u5bf9\u4e8e\u6d4f\u89c8\u5668\u7684html\uff0c\u7f3a\u4e4fscript\u6807\u7b7e\u6765\u5f15\u5165\u6587\u4ef6\uff0c\u5b8c\u5168\u4f9d\u8d56\u672c\u5730\u6587\u4ef6\u7cfb\u7edf\u7684js\u6587\u4ef6\u3002\u4e8e\u662fNodeJS\u6309\u7167CommonJS\u89c4\u8303\u5b9e\u73b0\u4e86\u4e00\u5957\u6a21\u5757\u7cfb\u7edf<\/p>\n\n\n\n<p>2015\u5e74ES2015\u89c4\u8303\u53d1\u5e03\uff0c\u5230\u4e86\u8fd9\u4e2a\u65f6\u5019\uff0cJS\u624d\u5bf9\u6a21\u5757\u7cfb\u7edf\u6709\u4e86\u6b63\u5f0f\u6807\u51c6\uff0c\u6309\u7167\u8fd9\u79cd\u6807\u51c6\u6253\u9020\u7684\u6a21\u5757\u7cfb\u7edf\u53eb\u4f5cESM\u7cfb\u7edf\uff0c\u4ed6\u8ba9\u6d4f\u89c8\u5668\u548c\u670d\u52a1\u7aef\u5728\u6a21\u5757\u7684\u7ba1\u7406\u65b9\u5f0f\u4e0a\u66f4\u52a0\u4e00\u81f4<\/p>\n\n\n\n<p><strong>CommonJS\u6a21\u5757<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>CommonJS\u89c4\u5212\u4e2d\u6709\u4e24\u4e2a\u57fa\u672c\u7406\u5ff5:<\/p>\n\n\n\n<ul><li>\u7528\u6237\u53ef\u4ee5\u901a\u8fc7requeire\u51fd\u6570\uff0c\u5f15\u5165\u672c\u5730\u6587\u4ef6\u7cfb\u7edf\u4e2d\u7684\u67d0\u4e2a\u6a21\u5757<\/li><li>\u901a\u8fc7exports\u548cmodule.exports\u4e24\u4e2a\u7279\u6b8a\u53d8\u91cf\uff0c\u5bf9\u5916\u53d1\u5e03\u80fd\u529b<\/li><\/ul>\n\n\n\n<p><strong>\u6a21\u5757\u52a0\u8f7d\u5668<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u4e0b\u9762\u6765\u7b80\u5355\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u6a21\u5757\u52a0\u8f7d\u5668<\/p>\n\n\n\n<p>\u9996\u5148\u662f\u52a0\u8f7d\u6a21\u5757\u5185\u5bb9\u7684\u51fd\u6570\uff0c\u6211\u4eec\u628a\u8fd9\u4e2a\u51fd\u6570\u653e\u5728\u79c1\u6709\u4f5c\u7528\u57df\u91cc\u8fb9\u907f\u514d\u6c61\u67d3\u5168\u5c40\u73af\u5883\uff0c\u7136\u540eeval\u8fd0\u884c\u8be5\u51fd\u6570<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function loadModule(filname, module, require) {\n  const wrappedSrc = `\n    (function (module, exports, require) {\n      ${fs.readFileSync(filename, 'utf-8')}\n    })(module, module.exports, require)\n  `\n  eval(wrappedSrc)\n}\n<\/pre>\n\n\n\n<p>\u5728\u4ee3\u7801\u4e2d\u6211\u4eec\u901a\u8fc7\u540c\u6b65\u65b9\u6cd5`readFileSync`\u6765\u8bfb\u53d6\u4e86\u6a21\u5757\u5185\u5bb9\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u5728\u8c03\u7528\u6587\u4ef6\u7cfb\u7edfAPI\u65f6\uff0c\u4e0d\u5e94\u8be5\u4f7f\u7528\u540c\u6b65\u7248\u672c\uff0c\u4f46\u662f\u6b64\u5904\u786e\u5b9e\u662f\u4f7f\u7528\u4e86\u8fd9\u4e2a\u65b9\u5f0f\uff0cCommonjs\u901a\u8fc7\u540c\u6b65\u64cd\u4f5c\uff0c\u6765\u4fdd\u8bc1\u591a\u4e2a\u6a21\u5757\u80fd\u591f\u5b89\u88c5\u6b63\u5e38\u7684\u4f9d\u8d56\u987a\u5e8f\u5f97\u5230\u5f15\u5165<\/p>\n\n\n\n<p><strong>\u73b0\u5728\u5728\u5b9e\u73b0`require`\u51fd\u6570<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function require(moduleName) {\n  const id = require.resolve(moduleName);\n  if (require.cache[id]) {\n    return require.cache[id].exports\n  }\n\n  \/\/ \u6a21\u5757\u7684\u5143\u6570\u636e\n\n  const module = {\n    exports: {},\n    id,\n  }\n\n  require.cache[id] = module;\n\n  loadModule(id, module, require);\n\n  \/\/ \u8fd4\u56de\u5bfc\u51fa\u7684\u53d8\u91cf\n  return module.exports\n}\n\nrequire.cache = {};\nrequire.resolve = (moduleName) => {\n  \/\/ \u6839\u636eModuleName\u89e3\u6790\u5b8c\u6574\u7684\u6a21\u5757ID\n}<\/pre>\n\n\n\n<p>\u4e0a\u9762\u5b9e\u73b0\u4e86\u4e00\u4e2a\u7b80\u5355\u7684`require`\u51fd\u6570\uff0c\u8fd9\u4e2a\u81ea\u5236\u7684\u6a21\u5757\u7cfb\u7edf\u6709\u51e0\u4e2a\u6b65\u9aa4\u9700\u8981\u89e3\u91ca<\/p>\n\n\n\n<ul><li>1.\u8f93\u5165\u6a21\u5757\u7684ModuleName\u4ee5\u540e\uff0c\u9996\u5148\u8981\u89e3\u6790\u51fa\u6a21\u5757\u7684\u5b8c\u6574\u8def\u5f84(\u5982\u4f55\u89e3\u6790\u540e\u9762\u4f1a\u8bb2\u5230)\uff0c\u7136\u540e\u628a\u8fd9\u4e2a\u7ed3\u679c\u4fdd\u5b58\u5728id\u7684\u53d8\u91cf\u4e4b\u4e2d<\/li><\/ul>\n\n\n\n<ul><li>2. \u5982\u679c\u8be5\u6a21\u5757\u5df2\u7ecf\u88ab\u52a0\u8f7d\u8fc7\u4e86\uff0c\u4f1a\u7acb\u523b\u8fd4\u56de\u7f13\u5b58\u4e2d\u7684\u7ed3\u679c<\/li><li>3. \u5982\u679c\u8be5\u6a21\u677f\u6ca1\u6709\u88ab\u52a0\u8f7d\u8fc7\uff0c\u90a3\u4e48\u5c31\u914d\u7f6e\u4e00\u5957\u73af\u5883\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u5148\u521b\u5efa\u4e00\u4e2a`module`\u53d8\u91cf\uff0c\u8ba9\u4ed6\u5305\u542b\u4e00\u4e2aexports\u7684\u5c5e\u6027\u3002\u8fd9\u4e2a\u5bf9\u8c61\u7684\u5185\u5bb9\uff0c\u5c06\u7531\u6a21\u5757\u5728\u5bfc\u51faAPI\u65f6\u6240\u4f7f\u7528\u7684\u7684\u90a3\u4e9b\u4ee3\u7801\u6765\u586b\u5145<\/li><li>4. \u5c06module\u5bf9\u8c61\u7f13\u5b58\u8d77\u6765<\/li><li>5. \u6267\u884c`loadModule`\u51fd\u6570\uff0c\u4f20\u5165\u521a\u5efa\u7acb\u7684module\u5bf9\u8c61\uff0c\u901a\u8fc7\u51fd\u6570\u5c06\u53e6\u5916\u4e00\u4e2a\u6a21\u5757\u7684\u5185\u5bb9\u8fdb\u884c\u6302\u8f7d<\/li><li>5. \u8fd4\u56de\u53e6\u5916\u6a21\u5757\u7684\u5bfc\u51fa\u5185\u5bb9<\/li><\/ul>\n\n\n\n<p><strong>\u6a21\u5757\u89e3\u6790\u7b97\u6cd5<\/strong><\/p>\n\n\n\n<p>\u5728\u524d\u9762\u63d0\u5230\u89e3\u6790\u6a21\u5757\u7684\u5b8c\u6574\u8def\u5f84\uff0c\u6211\u4eec\u901a\u8fc7\u4f20\u5165\u6a21\u5757\u540d\uff0c\u6a21\u5757\u89e3\u6790\u51fd\u6570\u80fd\u591f\u8fd4\u56de\u6a21\u5757\u7684\u5bf9\u5e94\u7684\u5b8c\u6574\u8def\u5f84\uff0c\u63a5\u4e0b\u6765\u901a\u8fc7\u8def\u5f84\u6765\u52a0\u8f7d\u5bf9\u5e94\u6a21\u5757\u7684\u4ee3\u7801\uff0c\u5e76\u7528\u8fd9\u4e2a\u8def\u5f84\u6765\u6807\u8bc6\u6a21\u5757\u7684\u8eab\u4efd\u3002`resolve`\u51fd\u6570\u6240\u7528\u7684\u89e3\u6790\u51fd\u6570\u4e3b\u8981\u662f\u5904\u7406\u4ee5\u4e0b\u4e09\u79cd\u60c5\u51b5<\/p>\n\n\n\n<p>&#8211; **\u8981\u52a0\u8f7d\u7684\u662f\u4e0d\u662f\u6587\u4ef6\u6a21\u5757?** \u5982\u679cmoduleName\u4ee5\/\u5f00\u5934\uff0c\u90a3\u5c31\u89c6\u4e3a\u4e00\u6761\u7edd\u5bf9\u8def\u5f84\uff0c\u52a0\u8f7d\u65f6\u53ea\u9700\u8981\u5b89\u88c5\u8be5\u8def\u5f84\u539f\u6837\u8fd4\u56de\u5373\u53ef\u3002\u5982\u679cmoduleName\u4ee5`.\/`\u5f00\u5934\uff0c\u90a3\u4e48\u5c31\u5f53\u6210\u4e00\u6761\u76f8\u5bf9\u8def\u5f84\uff0c\u8fd9\u6837\u76f8\u5bf9\u8def\u5f84\u662f\u4ece\u8bf7\u6c42\u8f7d\u5165\u8be5\u6a21\u5757\u7684\u8fd9\u4e2a\u76ee\u5f55\u7b97\u8d77\u7684<\/p>\n\n\n\n<p>&#8211; **\u8981\u52a0\u8f7d\u7684\u662f\u4e0d\u662f\u6838\u5fc3\u6a21\u5757** \u5982\u679c`moduleName`\u4e0d\u662f\u4ee5`\/`\u6216\u8005`.\/`\u5f00\u5934\uff0c\u90a3\u4e48\u7b97\u6cd5\u4f1a\u9996\u5148\u5c1d\u8bd5\u5728`NodeJS`\u7684\u6838\u5fc3\u6a21\u5757\u53bb\u5bfb\u627e<\/p>\n\n\n\n<p>&#8211; **\u8981\u52a0\u8f7d\u7684\u662f\u4e0d\u662f\u5305\u6a21\u5757** \u5982\u679c\u6ca1\u6709\u627e\u5230`moduleName`\u5339\u914d\u7684\u6838\u5fc3\u6a21\u5757\uff0c\u90a3\u5c31\u4ece\u53d1\u51fa\u52a0\u8f7d\u8bf7\u6c42\u7684\u8fd9\u4e2a\u6a21\u5757\u5f00\u59cb\uff0c\u9010\u5c42\u5411\u4e0a\u641c\u5bfb\u540d\u4e3a`node_modules`\u7684\u964c\u8def\uff0c\u770b\u770b\u91cc\u8fb9\u6709\u6ca1\u6709\u80fd\u591f\u4e0e`moduleName`\u5339\u914d\u7684\u6a21\u5757\uff0c\u5982\u679c\u6709\u5c31\u8f7d\u5165\u8be5\u6a21\u5757\u3002\u5982\u679c\u8fd8\u6ca1\u6709\uff0c\u5c31\u6cbf\u7740\u76ee\u5f55\u7ee7\u7eed\u7ebf\u4e0a\u8d70\uff0c\u5e76\u5728\u76f8\u5e94\u7684`node_modules`\u76ee\u5f55\u4e2d\u641c\u5bfb\uff0c\u4e00\u76f4\u5230\u6587\u4ef6\u7cfb\u7edf\u7684\u6839\u76ee\u5f55<\/p>\n\n\n\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u5c31\u80fd\u5b9e\u73b0\u4e24\u4e2a\u6a21\u5757\u4f9d\u8d56\u4e0d\u540c\u7248\u672c\u7684\u5305\uff0c\u4f46\u662f\u4ecd\u7136\u80fd\u591f\u6b63\u5e38\u52a0\u8f7d<\/p>\n\n\n\n<p>\u4f8b\u5982\u4ee5\u4e0b\u76ee\u5f55\u7ed3\u6784:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">myApp\n    - index.js\n    - node_modules\n        - depA\n            - index.js\n        - depB\n            - index.js\n            - node_modules\n                - depA\n        - depC\n            - index.js\n            - node_modules\n                - depA<\/pre>\n\n\n\n<p>\u5728\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\u867d\u7136`myApp`\u3001`depB`\u3001`depC`\u90fd\u4f9d\u8d56\u4e86`depA`\u4f46\u662f\u52a0\u8f7d\u8fdb\u6765\u7684\u786e\u5b9e\u4e0d\u540c\u7684\u6a21\u5757\u3002\u6bd4\u5982\uff1a<\/p>\n\n\n\n<ul><li>&#8211; \u5728`\/myApp\/index.js`\u4e2d\uff0c\u52a0\u8f7d\u7684\u6765\u6e90\u662f`\/myApp\/node_modules\/depA`<\/li><li>&#8211; \u5728`\/myApp\/node_modules\/depB\/index.js`, \u52a0\u8f7d\u7684\u662f`\/myApp\/node_modules\/depB\/node_modules\/depA`<\/li><li>&#8211; \u5728`\/myApp\/node_modules\/depC\/index.js`, \u52a0\u8f7d\u7684\u662f`\/myApp\/node_modules\/depC\/node_modules\/depA`<\/li><\/ul>\n\n\n\n<p>NodeJs\u4e4b\u6240\u4ee5\u80fd\u591f\u628a\u4f9d\u8d56\u5173\u7cfb\u7ba1\u7406\u597d\uff0c\u5c31\u56e0\u4e3a\u5b83\u80cc\u540e\u6709\u6a21\u5757\u89e3\u6790\u7b97\u6cd5\u8fd9\u6837\u4e00\u4e2a\u6838\u5fc3\u7684\u90e8\u5206\uff0c\u80fd\u591f\u7ba1\u7406\u4e0a\u5343\u4e2a\u5305\uff0c\u800c\u4e0d\u4f1a\u53d1\u751f\u51b2\u7a81\u6216\u51fa\u73b0\u7248\u672c\u4e0d\u517c\u5bb9\u7684\u95ee\u9898<\/p>\n\n\n\n<p><strong>\u5faa\u73af\u4f9d\u8d56<\/strong><\/p>\n\n\n\n<p>\u5f88\u591a\u4eba\u89c9\u5f97\u5faa\u73af\u4f9d\u8d56\u662f\u7406\u8bba\u4e0a\u7684\u8bbe\u8ba1\u95ee\u9898\uff0c\u4f46\u662f\u8fd9\u79cd\u95ee\u9898\u5f88\u53ef\u80fd\u51fa\u73b0\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u6240\u4ee5\u5e94\u8be5\u77e5\u9053CommonJS\u5982\u4f55\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\u7684\u3002\u662f\u770b\u4e4b\u524d\u5b9e\u73b0\u7684require\u51fd\u6570\u5c31\u80fd\u591f\u610f\u8bc6\u5230\u5176\u4e2d\u7684\u98ce\u9669\u3002\u4e0b\u9762\u901a\u8fc7\u4e00\u4e2a\u4f8b\u5b50\u6765\u8bb2\u89e3<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/04\/image-127-1024x496.png\" alt=\"\" class=\"wp-image-10642\" width=\"404\" height=\"195\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-127-1024x496.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-127-300x145.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-127-768x372.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-127-830x402.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-127-230x111.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-127-350x170.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-127-480x233.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-127.png 1354w\" sizes=\"(max-width: 404px) 100vw, 404px\" \/><\/figure><\/div>\n\n\n\n<p>\u6709\u4e2amian.js\u7684\u6a21\u5757\uff0c\u9700\u8981\u4f9d\u8d56\u4e86a.js\u548cb.js\u4e24\u4e2a\u6a21\u5757\uff0c\u540c\u65f6a.js\u9700\u8981\u4f9d\u8d56b.js\uff0c\u4f46\u662fb.js\u53c8\u53cd\u8fc7\u6765\u4f9d\u8d56\u4e86a.js\uff0c\u8fd9\u5c31\u9020\u6210\u4e86\u5faa\u73af\u4f9d\u8d56\uff0c\u4e0b\u9762\u662f\u6e90\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ a.js\nexports.loaded = false;\nconst b = require('.\/b');\nmodule.exports = {\n  b,\n  loaded: true\n}\n\/\/ b.js\nexports.loaded = false;\nconst a = require('.\/a')\nmodule.exports = {\n  a,\n  loaded: false\n}\n\/\/ main.js\nconst a = require('.\/a');\nconst b = require('.\/b');\nconsole.log('A ->', JSON.stringify(a))\nconsole.log('B ->', JSON.stringify(b))<\/pre>\n\n\n\n<p>\u8fd0\u884c`main.js`\u4f1a\u5f97\u5230\u4ee5\u4e0b\u7ed3\u679c<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/04\/image-128-1024x134.png\" alt=\"\" class=\"wp-image-10643\" width=\"499\" height=\"64\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-128-1024x134.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-128-300x39.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-128-768x100.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-128-830x108.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-128-230x30.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-128-350x46.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-128-480x63.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-128.png 1334w\" sizes=\"(max-width: 499px) 100vw, 499px\" \/><\/figure><\/div>\n\n\n\n<p>\u4ece\u7ed3\u679c\u53ef\u4ee5\u770b\u5230\uff0cCommonJS\u5728\u5faa\u73af\u4f9d\u8d56\u6240\u5f15\u53d1\u7684\u98ce\u9669\u3002b\u6a21\u5757\u5bfc\u5165a\u6a21\u5757\u7684\u65f6\u5019\uff0c\u5185\u5bb9\u5e76\u4e0d\u662f\u5b8c\u6574\u7684\uff0c\u5177\u4f53\u6765\u8bf4\u4ed6\u53ea\u662f\u53cd\u5e94\u4e86b.js\u6a21\u5757\u8bf7\u6c42`a.js`\u6a21\u5757\u65f6\uff0c\u8be5\u6a21\u5757\u6240\u5904\u7684\u72b6\u6001\uff0c\u800c\u65e0\u6cd5\u53cd\u5e94`a.js`\u6a21\u5757\u6700\u7ec8\u52a0\u8f7d\u5b8c\u6bd5\u7684\u4e00\u4e2a\u72b6\u6001<\/p>\n\n\n\n<p>\u4e0b\u9762\u7528\u4e00\u4e2a\u793a\u4f8b\u56fe\u6765\u8868\u793a\u8fd9\u4e2a\u8fc7\u7a0b<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/04\/image-129-985x1024.png\" alt=\"\" class=\"wp-image-10644\" width=\"415\" height=\"431\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-129-985x1024.png 985w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-129-289x300.png 289w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-129-768x798.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-129-830x863.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-129-230x239.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-129-350x364.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-129-480x499.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-129.png 1418w\" sizes=\"(max-width: 415px) 100vw, 415px\" \/><\/figure><\/div>\n\n\n\n<p>\u4e0b\u9762\u662f\u5177\u4f53\u7684\u6d41\u7a0b\u89e3\u91ca<\/p>\n\n\n\n<ul><li>1. \u6574\u4e2a\u6d41\u7a0b\u4ecemain.js\u5f00\u59cb\uff0c\u8fd9\u4e2a\u6a21\u5757\u4e00\u5f00\u59cb\u5f00\u59cb\u5bfc\u5165a.js\u6a21\u5757<\/li><li>2. a.js\u9996\u5148\u8981\u505a\u7684\uff0c\u662f\u5bfc\u51fa\u4e00\u4e2a\u540d\u4e3aloaded\u7684\u503c\uff0c\u5e76\u628a\u8be5\u503c\u8bbe\u4e3afalse<\/li><li>3. a.js\u6a21\u5757\u8981\u6c42\u5bfc\u5165b.js\u6a21\u5757<\/li><li>4. \u4e0ea.js\u7c7b\u4f3c\uff0cb.js\u9996\u5148\u4e5f\u662f\u5bfc\u51faloaded\u4e3afalse\u7684\u53d8\u91cf<\/li><li>5. b.js\u7ee7\u7eed\u6267\u884c\uff0c\u9700\u8981\u5bfc\u5165a.js<\/li><li>6. \u7531\u4e8e\u7cfb\u7edf\u5df2\u7ecf\u5f00\u59cb\u5904\u7406a.js\u6a21\u5757\u4e86\uff0c\u6240\u4ee5b.js\u4f1a\u628aa.js\u5df2\u7ecf\u5bfc\u51fa\u7684\u5185\u5bb9\uff0c\u7acb\u5373\u590d\u5236\u5230\u672c\u6a21\u5757\u4e2d<\/li><li>7. b.js\u4f1a\u628a\u81ea\u5df1\u5bfc\u51fa\u7684loaded\u503c\u6539\u4e3afalse<\/li><li>8. \u7531\u4e8eb\u5df2\u7ecf\u6267\u884c\u5b8c\u6210\uff0c\u63a7\u5236\u6743\u4f1a\u56de\u5230a.js\uff0c\u4ed6\u4f1a\u628ab.js\u6a21\u5757\u7684\u72b6\u6001\u62f7\u8d1d\u4e00\u4efd<\/li><li>9. a.js\u7ee7\u7eed\u6267\u884c\uff0c\u4fee\u6539\u5bfc\u51fa\u503cloaded\u4e3atrue<\/li><li>10. \u6700\u540e\u5c31\u6267\u884cmain.js<\/li><\/ul>\n\n\n\n<p>\u4e0a\u9762\u53ef\u4ee5\u770b\u5230\u7531\u4e8e\u662f\u540c\u6b65\u6267\u884c\uff0c\u5bfc\u81f4b.js\u5bfc\u5165\u7684a.js\u6a21\u5757\u5e76\u4e0d\u662f\u5b8c\u6574\u7684\uff0c\u65e0\u6cd5\u53cd\u5e94b.js\u7684\u6700\u7ec8\u5e94\u6709\u7684\u72b6\u6001\u3002<\/p>\n\n\n\n<p>\u5728\u4e0a\u9762\u4f8b\u5b50\u4e2d\u53ef\u4ee5\u770b\u5230\uff0c\u5faa\u73af\u4f9d\u8d56\u6240\u4ea7\u751f\u7684\u7684\u7ed3\u679c\uff0c\u8fd9\u5bf9\u5927\u578b\u9879\u76ee\u6765\u8bf4\uff0c\u66f4\u52a0\u4e25\u91cd\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528\u65b9\u6cd5\u5c31\u6bd4\u8f83\u7b80\u5355\u4e86\uff0c\u7bc7\u5e45\u6709\u9650\u5c31\u4e0d\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\u8fdb\u884c\u8bb2\u89e3\u4e86<\/p>\n\n\n\n<p><strong>ESM<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>ESM\u662fECMAScript 2015\u89c4\u8303\u7684\u4e00\u90e8\u5206\uff0c\u8fd9\u4efd\u89c4\u8303\u7ed9Javascript\u5236\u5b9a\u4e86\u7edf\u4e00\u7684\u6a21\u5757\u7cfb\u7edf\uff0c\u4ee5\u9002\u5e94\u5404\u79cd\u6267\u884c\u73af\u5883\u3002ESM\u548cCommonJS\u7684\u4e00\u9879\u91cd\u8981\u533a\u522b\uff0c\u5728\u4e8e\u5728ES\u6a21\u5757\u662f\u9759\u6001\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\u5f15\u5165\u6a21\u5757\u7684\u8bed\u53e5\u5fc5\u987b\u8981\u5199\u5728\u6700\u9876\u5c42\u3002\u53e6\u5916\u53d7\u5f15\u7528\u7684\u6a21\u5757\u53ea\u80fd\u4f7f\u7528\u5e38\u91cf\u5b57\u7b26\u4e32\uff0c\u4e0d\u80fd\u4f9d\u8d56\u9700\u8981\u8fd0\u884c\u671f\u52a8\u6001\u6c42\u503c\u7684\u8868\u8fbe\u5f0f\u3002<\/p>\n\n\n\n<p>\u6bd4\u5982\u6211\u4eec\u4e0d\u80fd\u901a\u8fc7\u4e0b\u9762\u65b9\u5f0f\u6765\u5f15\u5165ES\u6a21\u5757<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">if (condition) {\n  import module1 from 'module1'\n} else {\n  import module2 from 'module2'\n}<\/pre>\n\n\n\n<p>\u800cCommonJS\u80fd\u591f\u6839\u636e\u6761\u4ef6\u5bfc\u5165\u4e0d\u540c\u7684\u6a21\u5757<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">let module = null\nif (condition) {\n  module = require(\"module1\")\n} else {\n  module = require(\"module2\")\n}<\/pre>\n\n\n\n<p>\u770b\u8d77\u6765\u76f8\u5bf9CommonJS\u66f4\u4e25\u683c\u4e86\u4e00\u4e9b\uff0c\u4f46\u662f\u6b63\u662f\u56e0\u4e3a\u8fd9\u79cd\u9759\u6001\u5f15\u5165\u673a\u5236\uff0c\u6211\u4eec\u80fd\u591f\u5bf9\u4f9d\u8d56\u5173\u7cfb\u8fdb\u884c\u9759\u6001\u5206\u6790\uff0c\u53bb\u9664\u4e0d\u4f1a\u6267\u884c\u7684\u903b\u8f91\uff0c\u8fd9\u4e2a\u5c31\u53eb`tree-shaking`<\/p>\n\n\n\n<p><strong>\u6a21\u5757\u52a0\u8f7d\u8fc7\u7a0b<\/strong><\/p>\n\n\n\n<p>\u8981\u60f3\u7406\u89e3ESM\u7cfb\u7edf\u7684\u8fd0\u4f5c\u539f\u7406\uff0c\u4ee5\u53ca\u5b83\u5904\u7406\u5faa\u73af\u4f9d\u8d56\u7684\u5173\u7cfb\uff0c\u6211\u4eec\u9700\u8981\u660e\u767d\u7cfb\u7edf\u662f\u5982\u4f55\u89e3\u6790\u5e76\u6267\u884cJavascript\u4ee3\u7801<\/p>\n\n\n\n<p><strong>\u8f7d\u5165\u6a21\u5757\u7684\u5404\u4e2a\u9636\u6bb5<\/strong><\/p>\n\n\n\n<p>\u89e3\u91ca\u5668\u7684\u76ee\u6807\u662f\u6784\u5efa\u4e00\u5f20\u56fe\u6765\u63cf\u8ff0\u6240\u8981\u8f7d\u5165\u7684\u6a21\u5757\u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb\uff0c\u8fd9\u79cd\u56fe\u4e5f\u53eb\u505a\u4f9d\u8d56\u56fe\u3002<\/p>\n\n\n\n<p>\u89e3\u91ca\u5668\u6b63\u662f\u901a\u8fc7\u8fd9\u79cd\u4f9d\u8d56\u56fe\uff0c\u6765\u5224\u65ad\u6a21\u5757\u7684\u4f9d\u8d56\u5173\u7cfb\uff0c\u5e76\u51b3\u5b9a\u81ea\u5df1\u5e94\u8be5\u6309\u7167\u4ec0\u4e48\u987a\u5e8f\u53bb\u6267\u884c\u4ee3\u7801\u3002\u4f8b\u5982\u6211\u4eec\u9700\u8981\u6267\u884c\u67d0\u4e2ajs\u6587\u4ef6\uff0c\u90a3\u4e48\u89e3\u91ca\u5668\u4f1a\u4ece\u5165\u53e3\u5f00\u59cb\uff0c\u5bfb\u627e\u6240\u6709\u7684import\u8bed\u53e5\uff0c\u5982\u679c\u5728\u5bfb\u627e\u8fc7\u7a0b\u4e2d\u53c8\u9047\u5230\u4e86import\u8bed\u53e5\uff0c\u90a3\u5c31\u4f1a\u4ee5\u6df1\u5ea6\u4f18\u5148\u7684\u65b9\u5f0f\u9012\u5f52\uff0c\u76f4\u5230\u6240\u6709\u7684\u4ee3\u7801\u90fd\u89e3\u6790\u5b8c\u6bd5\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u8fc7\u7a0b\u53ef\u7ec6\u5206\u4e3a\u4e09\u4e2a\u8fc7\u7a0b:<\/p>\n\n\n\n<ul><li>1. \u5256\u6790\uff1a \u627e\u5230\u6240\u6709\u7684\u5f15\u5165\u8bed\u53e5\uff0c\u5e76\u9012\u5f52\u4ece\u76f8\u5173\u6587\u4ef6\u4e2d\u52a0\u8f7d\u6bcf\u4e2a\u6a21\u5757\u7684\u5185\u5bb9<\/li><li>2. \u5b9e\u4f8b\u5316\uff1a \u9488\u5bf9\u67d0\u4e2a\u5bfc\u51fa\u7684\u5b9e\u4f53\uff0c\u5728\u5185\u5b58\u4e2d\u4fdd\u7559\u4e00\u4e2a\u5e26\u540d\u79f0\u7684\u5f15\u5165\uff0c\u4f46\u6682\u4e14\u4e0d\u7ed9\u4ed6\u8d4b\u503c\u3002\u6b64\u65f6\u8fd8\u8981\u6839\u636eimport\u548cexport\u5173\u952e\u5b57\u5efa\u7acb\u4f9d\u8d56\u5173\u7cfb\uff0c\u6b64\u65f6\u4e0d\u6267\u884cjs\u4ee3\u7801<\/li><li>3. \u6267\u884c\uff1a\u5230\u4e86\u8fd9\u4e2a\u9636\u6bb5\uff0cNodeJS\u5f00\u59cb\u6267\u884c\u4ee3\u7801\uff0c\u8fd9\u80fd\u591f\u8ba9\u5b9e\u9645\u5bfc\u51fa\u7684\u5b9e\u4f53\uff0c\u80fd\u591f\u83b7\u5f97\u5b9e\u9645\u7684\u53d6\u503c<\/li><\/ul>\n\n\n\n<p>\u5728CommonJS\u4e2d\uff0c\u662f\u8fb9\u89e3\u6790\u4f9d\u8d56\uff0c\u4e00\u8fb9\u6267\u884c\u6587\u4ef6\u3002\u6240\u4ee5\u5f53\u770b\u5230require\u7684\u65f6\u5019\uff0c\u5c31\u4ee3\u8868\u524d\u9762\u7684\u4ee3\u7801\u5df2\u7ecf\u6267\u884c\u5b8c\u6210\u3002\u56e0\u4e3arequire\u64cd\u4f5c\u4e0d\u4e00\u5b9a\u8981\u5728\u6587\u4ef6\u5f00\u5934\uff0c\u800c\u662f\u53ef\u4ee5\u51fa\u73b0\u5728\u4efb\u52a1\u5730\u65b9<\/p>\n\n\n\n<p>\u4f46\u662fESM\u7cfb\u7edf\u4e0d\u540c\uff0c\u8fd9\u4e09\u4e2a\u9636\u6bb5\u662f\u5206\u5f00\u7684\uff0c\u5b83\u5fc5\u987b\u5148\u628a\u4f9d\u8d56\u56fe\u5b8c\u6574\u7684\u6784\u9020\u51fa\u6765\uff0c\u7136\u540e\u624d\u5f00\u59cb\u6267\u884c\u4ee3\u7801<\/p>\n\n\n\n<p><strong>\u5faa\u73af\u4f9d\u8d56<\/strong><\/p>\n\n\n\n<p>\u5728\u4e4b\u524d\u63d0\u5230\u7684CommonJS\u5faa\u73af\u4f9d\u8d56\u7684\u4f8b\u5b50\uff0c\u4f7f\u7528ESM\u7684\u65b9\u5f0f\u8fdb\u884c\u6539\u9020<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ a.js\nimport * as bModule from '.\/b.js';\nexport let loaded = false;\nexport const b = bModule;\nloaded = true;\n\/\/ b.js\nimport * as aModule from '.\/b.js';\nexport let loaded = false;\nexport const a = aModule;\nloaded = true;\n\/\/ main.js\nimport * as a from '.\/a.js';\nimport * as b from '.\/b.js';\nconsole.log(\"A =>\", a)\nconsole.log(\"B =>\", b)<\/pre>\n\n\n\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\u8fd9\u91cc\u4e0d\u80fd\u662f\u7528`JSON.strinfy`\u65b9\u6cd5\uff0c\u56e0\u4e3a\u8fd9\u91cc\u4f7f\u7528\u4e86\u5faa\u73af\u4f9d\u8d56<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/04\/image-130.png\" alt=\"\" class=\"wp-image-10646\" width=\"506\" height=\"245\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-130.png 990w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-130-300x145.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-130-768x372.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-130-830x402.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-130-230x112.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-130-350x170.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-130-480x233.png 480w\" sizes=\"(max-width: 506px) 100vw, 506px\" \/><\/figure><\/div>\n\n\n\n<p>\u5728\u4e0a\u9762\u6267\u884c\u7ed3\u679c\u4e2d\u53ef\u4ee5\u770b\u5230a.js\u548cb.js\u90fd\u80fd\u591f\u5b8c\u6574\u7684\u89c2\u5bdf\u5230\u5bf9\u65b9\uff0c\u4e0d\u540c\u4e0eCommonJS\uff0c\u6709\u6a21\u5757\u62ff\u5230\u7684\u72b6\u6001\u662f\u4e0d\u5b8c\u6574\u7684\u72b6\u6001\u3002<\/p>\n\n\n\n<p><strong>\u5256\u6790<\/strong><\/p>\n\n\n\n<p>\u4e0b\u9762\u6765\u89e3\u6790\u4e00\u4e0b\u5176\u4e2d\u7684\u8fc7\u7a0b\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/04\/image-131-634x1024.png\" alt=\"\" class=\"wp-image-10647\" width=\"244\" height=\"394\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-131-634x1024.png 634w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-131-186x300.png 186w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-131-768x1241.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-131-230x372.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-131-350x566.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-131-480x776.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-131.png 812w\" sizes=\"(max-width: 244px) 100vw, 244px\" \/><\/figure><\/div>\n\n\n\n<p>\u5df2\u4e0a\u56fe\u4e3a\u4f8b\uff1a<\/p>\n\n\n\n<ul><li>1. \u4ecemain.js\u5f00\u59cb\u5256\u6790\uff0c\u9996\u5148\u53d1\u73b0\u4e86\u4e00\u6761import\u8bed\u53e5\uff0c\u7136\u540e\u8fdb\u5165a.js<\/li><li>2. \u4ecea.js\u5f00\u59cb\u6267\u884c\uff0c\u53d1\u73b0\u4e86\u53e6\u5916\u4e00\u6761import\u8bed\u53e5\uff0c\u6267\u884cb.js<\/li><li>3. \u5728b.js\u5f00\u59cb\u6267\u884c\uff0c\u53d1\u73b0\u4e86\u4e00\u6761import\u8bed\u53e5\uff0c\u5f15\u5165a.js\uff0c\u56e0\u4e3a\u4e4b\u524da.js\u5df2\u7ecf\u88ab\u4f9d\u8d56\u8fc7\uff0c\u6211\u4eec\u4e0d\u4f1a\u518d\u53bb\u6267\u884c\u8fd9\u6761\u8def\u5f84<\/li><li>4. b.js\u7ee7\u7eed\u5f80\u4e0b\u6267\u884c\uff0c\u53d1\u73b0\u6ca1\u6709\u522b\u7684import\u8bed\u53e5\u3002\u56de\u5230a.js\u4e4b\u540e\uff0c\u4e5f\u53d1\u73b0\u6ca1\u6709\u5176\u4ed6\u7684import\u8bed\u53e5\uff0c\u7136\u540e\u76f4\u63a5\u56de\u5230main.js\u5165\u53e3\u6587\u4ef6\u3002\u7ee7\u7eed\u5f80\u4e0b\u6267\u884c\uff0c\u53d1\u73b0\u8981\u6c42\u5f15\u5165b.js\uff0c\u4f46\u662f\u8fd9\u4e2a\u6a21\u5757\u4e4b\u524d\u88ab\u8bbf\u95ee\u8fc7\u4e86\uff0c\u56e0\u6b64\u8fd9\u6761\u8def\u5f84\u4e0d\u4f1a\u6267\u884c<\/li><\/ul>\n\n\n\n<p>\u7ecf\u8fc7\u6df1\u5ea6\u4f18\u5148\u7684\u65b9\u5f0f\uff0c\u6a21\u5757\u4f9d\u8d56\u5173\u7cfb\u56fe\u5df2\u7ecf\u5f62\u6210\u4e00\u4e2a\u6811\u72b6\u56fe\uff0c\u7136\u540e\u89e3\u91ca\u5668\u5728\u901a\u8fc7\u8fd9\u4e2a\u4f9d\u8d56\u56fe\u6267\u884c\u4ee3\u7801<\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u9636\u6bb5\uff0c\u89e3\u91ca\u5668\u8981\u4ece\u5165\u53e3\u70b9\u5f00\u59cb\uff0c\u5f00\u59cb\u5206\u6790\u5404\u6a21\u5757\u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb\u3002\u8fd9\u4e2a\u9636\u6bb5\u89e3\u91ca\u5668\u53ea\u5173\u5fc3\u7cfb\u7edf\u7684import\u8bed\u53e5\uff0c\u5e76\u628a\u8fd9\u4e9b\u8bed\u53e5\u60f3\u8981\u5f15\u5165\u7684\u6a21\u5757\u7ed9\u52a0\u8f7d\u8fdb\u6765\uff0c\u5e76\u4ee5\u6df1\u5ea6\u4f18\u5148\u7684\u65b9\u5f0f\u63a2\u7d22\u4f9d\u8d56\u56fe\u3002\u6309\u7167\u8fd9\u79cd\u65b9\u6cd5\u904d\u5386\u4f9d\u8d56\u5173\u7cfb\uff0c\u5f97\u5230\u4e00\u79cd\u6811\u72b6\u7684\u7ed3\u6784<\/p>\n\n\n\n<p><strong>\u5b9e\u4f8b\u5316<\/strong><\/p>\n\n\n\n<p>\u5728\u8fd9\u4e00\u9636\u6bb5\uff0c\u89e3\u91ca\u5668\u4f1a\u4ece\u6811\u72b6\u7ed3\u6784\u7684\u5e95\u90e8\u5f00\u59cb\uff0c\u9010\u6e10\u5411\u9876\u90e8\u8d70\u3002\u6ca1\u8d70\u5230\u4e00\u4e2a\u6a21\u5757\uff0c\u5b83\u5c31\u4f1a\u5bfb\u627e\u8be5\u6a21\u5757\u6240\u8981\u5bfc\u51fa\u7684\u6240\u6709\u5c5e\u6027\uff0c\u5e76\u5728\u5185\u5b58\u4e2d\u6784\u5efa\u4e00\u5f20\u9690\u5c04\u8868\uff0c\u4ee5\u5b58\u653e\u6b64\u6a21\u5757\u6240\u8981\u5bfc\u51fa\u7684\u5c5e\u6027\u540d\u79f0\u4e0e\u8be5\u5c5e\u6027\u5373\u5c06\u62e5\u6709\u7684\u53d6\u503c<\/p>\n\n\n\n<p>\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/04\/image-132.png\" alt=\"\" class=\"wp-image-10648\" width=\"364\" height=\"276\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-132.png 1010w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-132-300x228.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-132-768x584.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-132-830x631.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-132-230x175.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-132-350x266.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-132-480x365.png 480w\" sizes=\"(max-width: 364px) 100vw, 364px\" \/><\/figure><\/div>\n\n\n\n<p>\u4ece\u4e0a\u56fe\u53ef\u4ee5\u770b\u5230\uff0c\u6a21\u5757\u662f\u6309\u7167\u4ec0\u4e48\u987a\u5e8f\u6765\u5b9e\u4f8b\u5316\u7684<\/p>\n\n\n\n<ul><li>1. \u89e3\u91ca\u5668\u9996\u5148\u4eceb.js\u6a21\u5757\u5f00\u59cb\uff0c\u5b83\u53d1\u73b0\u8fd9\u4e2a\u6a21\u5757\u8981\u5bfc\u51faloaded\u548ca<\/li><li>2. \u7136\u540e\u89e3\u91ca\u5668\u53c8\u5206\u6790a.js\u6a21\u5757\uff0c\u4ed6\u53d1\u73b0\u8fd9\u4e2a\u6a21\u5757\u8981\u5bfc\u51faloaded\u548cb<\/li><li>3. \u6700\u540e\u5206\u6790main.js\u6a21\u5757\uff0c\u4ed6\u53d1\u73b0\u8fd9\u4e2a\u6a21\u5757\u4e0d\u5bfc\u51fa\u4efb\u4f55\u529f\u80fd<\/li><li>4. \u5b9e\u4f8b\u5316\u9636\u6bb5\u6240\u6784\u9020\u7684\u8fd9\u5957exports\u9690\u5c04\u56fe\uff0c\u53ea\u8bb0\u5f55\u5bfc\u51fa\u7684\u540d\u79f0\u4e0e\u8be5\u540d\u79f0\u5373\u5c06\u62e5\u6709\u7684\u503c\u4e4b\u95f4\u5173\u7cfb\uff0c\u81f3\u4e8e\u8fd9\u4e2a\u503c\u672c\u8eab\uff0c\u65e2\u4e0d\u5728\u672c\u9636\u6bb5\u521d\u59cb\u5316\u3002<\/li><\/ul>\n\n\n\n<p>\u8d70\u5b8c\u4e0a\u8ff0\u6d41\u7a0b\u540e\uff0c\u89e3\u6790\u5668\u8fd8\u9700\u8981\u5728\u6267\u884c\u4e00\u904d\uff0c\u8fd9\u6b21\u4ed6\u4f1a\u628a\u5404\u6a21\u5757\u6240\u5bfc\u51fa\u7684\u540d\u79f0\u4e0e\u5f15\u5165\u8fd9\u4e9b\u7684\u90a3\u4e9b\u6a21\u5757\u5173\u8054\u8d77\u6765\uff0c\u5982\u4e0b\u56fe\u6240\u793a:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/04\/image-133.png\" alt=\"\" class=\"wp-image-10649\" width=\"467\" height=\"229\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-133.png 1006w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-133-300x147.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-133-768x377.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-133-830x408.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-133-230x113.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-133-350x172.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-133-480x236.png 480w\" sizes=\"(max-width: 467px) 100vw, 467px\" \/><\/figure><\/div>\n\n\n\n<p>\u8fd9\u6b21\u7684\u6b65\u9aa4\u4e3a:<\/p>\n\n\n\n<ul><li>1. \u6a21\u5757b.js\u8981\u4e0e\u6a21\u5757b.js\u6240\u5bfc\u51fa\u7684\u5185\u5bb9\u76f8\u8fde\u63a5\uff0c\u8fd9\u6761\u94fe\u63a5\u53eb\u4f5caModule<\/li><li>2. \u6a21\u5757a.js\u8981\u4e0e\u6a21\u5757a.js\u6240\u5bfc\u51fa\u7684\u5185\u5bb9\u76f8\u8fde\u63a5\uff0c\u8fd9\u6761\u94fe\u63a5\u53eb\u4f5cbModule<\/li><li>3. \u6700\u540e\u6a21\u5757main.js\u8981\u4e0e\u6a21\u5757b.js\u6240\u5bfc\u51fa\u7684\u5185\u5bb9\u76f8\u8fde\u63a5<\/li><li>4. \u5728\u8fd9\u4e2a\u9636\u6bb5\uff0c\u6240\u6709\u7684\u503c\u5e76\u6ca1\u6709\u521d\u59cb\u5316\uff0c\u6211\u4eec\u53ea\u662f\u5efa\u7acb\u76f8\u5e94\u7684\u94fe\u63a5\uff0c\u80fd\u591f\u8ba9\u8fd9\u4e9b\u94fe\u63a5\u6307\u5411\u76f8\u5e94\u7684\u503c\uff0c\u81f3\u4e8e\u503c\u672c\u8eab\uff0c\u9700\u8981\u7b49\u5230\u4e0b\u4e00\u9636\u6bb5\u624d\u80fd\u786e\u5b9a<\/li><\/ul>\n\n\n\n<p><strong>\u6267\u884c<\/strong><\/p>\n\n\n\n<p>\u8fd9\u8fd9\u4e2a\u9636\u6bb5\uff0c\u7cfb\u7edf\u7ec8\u4e8e\u8981\u6267\u884c\u6bcf\u4efd\u6587\u4ef6\u91cc\u8fb9\u7684\u4ee3\u7801\u3002\u4ed6\u6309\u7167\u540e\u5e8f\u7684\u6df1\u5ea6\u4f18\u5148\u987a\u5e8f\uff0c\u7531\u4e0b\u800c\u4e0a\u7684\u8bbf\u95ee\u6700\u521d\u90a3\u5f20\u4f9d\u8d56\u56fe\uff0c\u5e76\u9010\u4e2a\u6267\u884c\u8bbf\u95ee\u5230\u7684\u6587\u4ef6\u3002\u5728\u672c\u4f8b\u4e2d\uff0cmain.js\u4f1a\u653e\u5728\u6700\u540e\u6267\u884c\u3002\u8fd9\u79cd\u6267\u884c\u7ed3\u679c\u4fdd\u8bc1\u4e86\uff0c\u7a0b\u5e8f\u5728\u8fd0\u884c\u4e3b\u903b\u8f91\u7684\u65f6\u5019\uff0c\u5404\u6a21\u5757\u6240\u5bfc\u51fa\u7684\u90a3\u4e9b\u503c\uff0c\u5168\u90e8\u5f97\u5230\u4e86\u521d\u59cb\u5316<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/04\/image-134.png\" alt=\"\" class=\"wp-image-10650\" width=\"388\" height=\"240\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-134.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-134-300x186.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-134-768x476.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-134-830x514.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-134-230x142.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-134-350x217.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-134-480x297.png 480w\" sizes=\"(max-width: 388px) 100vw, 388px\" \/><\/figure><\/div>\n\n\n\n<p>\u4ee5\u4e0a\u56fe\u5177\u4f53\u6b65\u9aa4\u4e3a:<\/p>\n\n\n\n<ul><li>1. \u4eceb.js\u5f00\u59cb\u6267\u884c\u3002\u9996\u5148\u8981\u6267\u884c\u7684\u8fd9\u884c\u4ee3\u7801\uff0c\u4f1a\u628a\u8be5\u6a21\u5757\u6240\u5bfc\u51fa\u7684loaded\u521d\u59cb\u5316\u4e3afalse<\/li><li>2. \u63a5\u4e0b\u6765\u5f80\u4e0b\u6267\u884c\uff0c\u4f1a\u628aaModule\u590d\u5236\u7ed9a\uff0c\u8fd9\u4e2a\u65f6\u5019a\u62ff\u5230\u7684\u662f\u4e00\u4e2a\u5f15\u7528\u503c\uff0c\u8fd9\u4e2a\u503c\u5c31\u662fa.js\u6a21\u5757<\/li><li>3. \u7136\u540e\u8bbe\u7f6eloaded\u7684\u503c\u4e3atrue\u3002\u8fd9\u4e2a\u65f6\u5019b\u6a21\u5757\u6240\u6709\u7684\u503c\u90fd\u5168\u90e8\u786e\u5b9a\u4e86\u4e0b\u6765<\/li><li>4. \u73b0\u5728\u6267\u884ca.js\u3002\u9996\u5148\u521d\u59cb\u5316\u5bfc\u51fa\u503cloaded\u4e3afalse<\/li><li>5. \u63a5\u4e0b\u6765\u5c06\u8be5\u6a21\u5757\u5bfc\u51fa\u7684b\u5c5e\u6027\u503c\u5f97\u5230\u521d\u59cb\u503c\uff0c\u8fd9\u4e2a\u503c\u662fbModule\u7684\u5f15\u7528<\/li><li>6. \u6700\u540e\u628aloaded\u7684\u503c\u6539\u4e3atrue\u3002\u5230\u4e86\u8fd9\u91cc\uff0c\u6211\u4eec\u5c31\u628aa.js\u6a21\u5757\u7cfb\u7edf\u5bfc\u51fa\u7684\u8fd9\u4e9b\u5c5e\u6027\u6240\u5bf9\u5e94\u7684\u503c\uff0c\u6700\u7ec8\u786e\u5b9a\u4e86\u4e0b\u6765<\/li><\/ul>\n\n\n\n<p>\u8d70\u5b8c\u8fd9\u4e9b\u6b65\u9aa4\u540e\uff0c\u7cfb\u7edf\u5c31\u53ef\u4ee5\u6b63\u5f0f\u6267\u884cmain.js\u6587\u4ef6\uff0c\u8fd9\u4e2a\u65f6\u5019\uff0c\u5404\u6a21\u5757\u6240\u5bfc\u51fa\u7684\u5c5e\u6027\u5168\u90fd\u5df2\u7ecf\u6c42\u503c\u5b8c\u6bd5\uff0c\u7531\u4e8e\u7cfb\u7edf\u662f\u901a\u8fc7\u5f15\u7528\u800c\u4e0d\u662f\u590d\u5236\u6765\u5f15\u5165\u6a21\u5757\uff0c\u6240\u4ee5\u5c31\u7b97\u6a21\u5757\u4e4b\u95f4\u6709\u5faa\u73af\u4f9d\u8d56\u5173\u7cfb\uff0c\u6bcf\u4e2a\u6a21\u5757\u8fd8\u662f\u80fd\u591f\u5b8c\u6574\u770b\u5230\u5bf9\u65b9\u7684\u6700\u7ec8\u72b6\u6001<\/p>\n\n\n\n<p><strong>CommonJS\u4e0eESM\u7684\u533a\u522b\u4e0e\u4ea4\u4e92\u4f7f\u7528<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u8fd9\u91cc\u8bb2CommonJS\u548cESM\u4e4b\u95f4\u51e0\u4e2a\u91cd\u8981\u7684\u533a\u522b\uff0c\u4ee5\u53ca\u5982\u4f55\u5728\u5fc5\u8981\u7684\u65f6\u5019\u642d\u914d\u4f7f\u7528\u8fd9\u4e24\u79cd\u6a21\u5757<\/p>\n\n\n\n<p><strong>ESM\u4e0d\u652f\u6301CommonJS\u63d0\u4f9b\u7684\u67d0\u4e9b\u5f15\u7528<\/strong><\/p>\n\n\n\n<p><span class=\"has-inline-color has-vivid-red-color\">CommonJS\u63d0\u4f9b\u4e00\u4e9b\u5173\u952e\u5f15\u7528\uff0c\u4e0d\u53d7ESM\u652f\u6301\uff0c\u8fd9\u5305\u62ec`require`\u3001`exports`\u3001`module.exports`\u3001`__filename`\u3001`__diranme`\u3002\u5982\u679c\u5728ES\u6a21\u5757\u4e2d\u4f7f\u7528\u8fd9\u4e9b\uff0c\u4f1a\u5230\u7a0b\u5e8f\u53d1\u751f\u5f15\u7528\u9519\u8bef\u7684\u95ee\u9898\u3002<\/span><\/p>\n\n\n\n<p>\u5728ESM\u7cfb\u7edf\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7import.meta\u8fd9\u4e2a\u7279\u6b8a\u5bf9\u8c61\u6765\u83b7\u53d6\u4e00\u4e2a\u5f15\u7528\uff0c\u8fd9\u4e2a\u5f15\u7528\u6307\u7684\u662f\u5f53\u524d\u6587\u4ef6\u7684URL\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u5c31\u662f\u901a\u8fc7import.meta.url\u8fd9\u79cd\u5199\u6cd5\uff0c\u6765\u83b7\u53d6\u5f53\u524d\u6a21\u5757\u7684\u6587\u4ef6\u8def\u5f84\uff0c\u8fd9\u4e2a\u8def\u5f84\u7c7b\u4f3c\u4e8e`file: \/\/\/path\/to\/current_module.js`\u3002\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u8fd9\u6761\u8def\u5f84\uff0c\u6784\u9020\u51fa`__filename`\u548c`__dirname`\u6240\u8868\u793a\u7684\u90a3\u4e24\u6761\u7edd\u5bf9\u8def\u5f84:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import { fileURLToPath } from 'url';\nimport { dirname } from 'path';\nconst __dirname = fileURLToPath(import.meta.url);\nconst __dirname = dirname(__filename);<\/pre>\n\n\n\n<p>CommonJS\u7684require\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u7528\u4e0b\u9762\u8fd9\u79cd\u65b9\u6cd5\uff0c\u5728ESM\u6a21\u5757\u91cc\u8fb9\u8fdb\u884c\u5b9e\u73b0:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import { createRequire } from 'module';\nconst require = createRequire(import.meta.url)<\/pre>\n\n\n\n<p>\u73b0\u5728\uff0c\u5c31\u53ef\u4ee5\u5728ES\u6a21\u5757\u7cfb\u7edf\u7684\u73af\u5883\u4e0b\uff0c\u7528\u8fd9\u4e2a`require()`\u51fd\u6570\u6765\u52a0\u8f7d`Commonjs`\u6a21\u5757<\/p>\n\n\n\n<p><strong>\u5728\u5176\u4e2d\u4e00\u4e2a\u6a21\u5757\u7cfb\u7edf\u4e2d\u4f7f\u7528\u53e6\u5916\u4e00\u4e2a\u6a21\u5757<\/strong><\/p>\n\n\n\n<p>\u5728\u4e0a\u9762\u63d0\u5230\uff0c\u5728ESM\u6a21\u5757\u4e2d\u4f7f\u7528`module.createRequire`\u51fd\u6570\u6765\u52a0\u8f7d`commonJS`\u6a21\u5757\u3002\u9664\u4e86\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u5176\u5b9e\u8fd8\u53ef\u4ee5\u901a\u8fc7import\u8bed\u8a00\u5f15\u5165CommonJS\u6a21\u5757\u3002\u4e0d\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u53ea\u4f1a\u5bfc\u51fa\u9ed8\u8ba4\u5bfc\u51fa\u7684\u5185\u5bb9;<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import pkg from 'commonJS-module'\nimport { method1 } from 'commonJS-module' \/\/ \u4f1a\u62a5\u9519<\/pre>\n\n\n\n<p>\u4f46\u662f\u53cd\u8fc7\u6765\u6ca1\u529e\u6cd5\uff0c\u6211\u4eec\u6ca1\u529e\u6cd5\u5728`commonJS`\u4e2d\u5f15\u5165`ESM`\u6a21\u5757<\/p>\n\n\n\n<p>\u6b64\u5916ESM\u4e0d\u652f\u6301\u628ajson\u6587\u4ef6\u5f53\u6210\u6a21\u5757\u8fdb\u884c\u5f15\u5165\uff0c\u8fd9\u5728commonjs\u5374\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0<\/p>\n\n\n\n<p>\u4e0b\u9762\u8fd9\u79cdimport\u8bed\u53e5\uff0c\u5c31\u4f1a\u62a5\u9519<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import json from 'data.json'<\/pre>\n\n\n\n<p>\u5982\u679c\u9700\u8981\u5f15\u5165json\u6587\u4ef6\uff0c\u8fd8\u9700\u8981\u501f\u52a9`createRequire`\u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import { createRequire } from 'module';\nconst require = createRequire(import.meta.url);\nconst data = require(\".\/data.json\");\nconsole.log(data)<\/pre>\n\n\n\n<p><strong>js \u548c .mjs \u6587\u4ef6\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<pre class=\"wp-block-preformatted\">\u6211\u5df2\u7ecf\u5f00\u59cb\u5728\u4e00\u4e2a\u57fa\u4e8e Node.js \u7684\u73b0\u6709\u9879\u76ee\u4e0a\u5de5\u4f5c\u3002\u6211\u53ea\u662f\u60f3\u4e86\u89e3\u6267\u884c\u6d41\u7a0b\uff0c\u5728\u90a3\u91cc\u6211\u9047\u5230\u4e86\u4e00\u4e9b*.mjs\u6587\u4ef6\u3002\u6211\u641c\u7d22\u4e86\u7f51\u7edc\uff0c\u53d1\u73b0\u8fd9\u4e9b\u662f\u57fa\u4e8e\u6a21\u5757\u7684 JS \u6587\u4ef6\u3002\u6211\u60f3\u77e5\u9053\u5b83\u4e0e*.js\u6587\u4ef6\u6709\u4ec0\u4e48\u4e0d\u540c\uff08\u5b83\u6709\u4ec0\u4e48\u597d\u5904\uff09\uff1f<\/pre>\n\n\n\n<p>Node.js \u7684\u539f\u59cb\u6a21\u5757\u7cfb\u7edf\u662f<a href=\"https:\/\/nodejs.org\/docs\/latest\/api\/modules.html\">CommonJs<\/a>\uff08\u4f7f\u7528<code>require<\/code>\u548c<code>module.exports<\/code>\uff09\u3002<\/p>\n\n\n\n<p>\u81ea\u4ece Node.js \u521b\u5efa\u4ee5\u6765\uff0c<a href=\"https:\/\/nodejs.org\/docs\/latest\/api\/esm.html\">ECMAScript \u6a21\u5757\u7cfb\u7edf<\/a>\uff08\u4f7f\u7528<code>import<\/code>\u548c<code>export<\/code>\uff09\u5df2\u7ecf\u6210\u4e3a\u6807\u51c6\uff0c\u5e76\u4e14 Node.js \u589e\u52a0\u4e86\u5bf9\u5b83\u7684\u652f\u6301\u3002<\/p>\n\n\n\n<p>Node.js \u4f1a\u5c06<code>.cjs<\/code>\u6587\u4ef6\u89c6\u4e3a CommonJS \u6a21\u5757\uff0c\u5c06<code>.mjs<\/code>\u6587\u4ef6\u89c6\u4e3a ECMAScript \u6a21\u5757\u3002\u5b83\u4f1a\u5c06<code>.js<\/code>\u6587\u4ef6\u89c6\u4e3a\u9879\u76ee\u7684\u9ed8\u8ba4\u6a21\u5757\u7cfb\u7edf\uff08\u9664\u975e<em>package.json<\/em>\u8bf4\u7684\u662f CommonJS <code>\"type\": \"module\",<\/code>\uff09\u3002<\/p>\n\n\n\n<p><strong>.MJS \u6587\u4ef6<\/strong><\/p>\n\n\n\n<ul><li>mjs \u662f EcmaScript \u6a21\u5757\u7684\u6269\u5c55<\/li><li>MJS \u6587\u4ef6\u662f\u5305\u542b\u7528\u4e8e Node.js \u5e94\u7528\u7a0b\u5e8f\u7684 ES \u6a21\u5757\uff08ECMAScript \u6a21\u5757\uff09\u7684\u6e90\u4ee3\u7801\u6587\u4ef6\u3002<\/li><li>MJS \u6587\u4ef6\u662f\u7528 JavaScript \u7f16\u5199\u7684\uff0c\u4e5f\u53ef\u80fd\u5728 Node.js \u4e0a\u4e0b\u6587\u4e4b\u5916\u4f7f\u7528 .JS \u6269\u5c55\u540d\u3002<\/li><li>ES \u6a21\u5757\u5141\u8bb8 Web \u548c\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u4eba\u5458\u5c06\u4ee3\u7801\u7ec4\u7ec7\u6210\u66f4\u5c0f\u7684\u53ef\u91cd\u7528\u7ec4\u4ef6\u3002<\/li><\/ul>\n\n\n\n<p>ECMAScript 6 (ES6) \u5f15\u5165\u4e86 ES Modules \u89c4\u8303\uff0c\u63d0\u4f9b\u4e86\u5728 JavaScript \u4e2d\u5b9e\u73b0\u6a21\u5757\u7684\u6807\u51c6\u3002\u622a\u81f3 2018 \u5e74\uff0c\u6240\u6709\u4e3b\u8981\u7684 Web \u6d4f\u89c8\u5668\u90fd\u652f\u6301 ES \u6a21\u5757\u3002<\/p>\n\n\n\n<p>\u7136\u800c\uff0c\u6a21\u5757\u5316 JavaScript \u7684\u6d41\u884c\u65e9\u4e8e ES6\u3002Node.js \u662f\u4e00\u79cd JavaScript \u8fd0\u884c\u65f6\u73af\u5883\uff0c\u4f7f\u7528 CommonJS \u4f5c\u4e3a\u6a21\u5757\u89c4\u8303\u3002\u56e0\u4e3a\u73b0\u6709\u7684\u5f88\u591a\u5e94\u7528\u90fd\u662f\u7528CommonJS\u6784\u5efa\u7684\uff0c\u6240\u4ee5Node.js\u5728\u6dfb\u52a0\u5bf9\u539f\u751fES\u6a21\u5757\u7684\u652f\u6301\u65f6\uff0c\u9887\u6709\u4e89\u8bae\u5730\u5f15\u5165\u4e86MJS\u6587\u4ef6\u6269\u5c55\u540d\u6765\u533a\u5206\u4e24\u8005\uff0c\u9632\u6b62\u5e94\u7528\u5d29\u6e83\u3002<\/p>\n\n\n\n<p><strong>\u4e3a\u6e05\u695a\u8d77\u89c1<\/strong>\u3002\u5bf9\u4e8e\u5f00\u53d1\u4eba\u5458\/\u4eba\u7c7b\uff0c\u5f88\u5bb9\u6613\u533a\u5206\u6a21\u5757\u6587\u4ef6\uff08.mjs\uff09\u548c\u666e\u901a\u7684 javascript \u6587\u4ef6\uff08.js\uff09&#8230;\u56e0\u4e3a\u5373\u4f7f\u60a8\u68c0\u67e5\u6587\u4ef6\u4e2d\u7684\u4ee3\u7801\u4e5f\u5e76\u4e0d\u603b\u662f\u5bb9\u6613\u786e\u5b9a\u3002<\/p>\n\n\n\n<p><strong>\u603b\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u672c\u6587\u4e3b\u8981\u8bb2\u89e3\u4e86NodeJS\u4e2d\u4e24\u79cd\u6a21\u5757\u7cfb\u7edf\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u901a\u8fc7\u4e86\u89e3\u8fd9\u4e9b\u539f\u56e0\u80fd\u591f\u5e2e\u5fd9\u6211\u4eec\u7f16\u5199\u907f\u514d\u4e00\u4e9b\u96be\u4ee5\u6392\u67e5\u7684\u95ee\u9898\u7684bug<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u6a21\u5757\u662fNode.js\u5e94\u7528\u7a0b\u5e8f\u7684\u57fa\u672c\u7ec4\u6210\u90e8\u5206\uff0c\u6587\u4ef6\u548c\u6a21\u5757\u662f\u4e00\u4e00\u5bf9\u5e94\u7684\uff0c\u4e00\u4e2aNodejs\u6a21\u5757\u5c31\u662f\u4e00\u4e2a\u6587\u4ef6\uff0c\u800c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/10638"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10638"}],"version-history":[{"count":6,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/10638\/revisions"}],"predecessor-version":[{"id":10653,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/10638\/revisions\/10653"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}