{"id":10654,"date":"2023-04-12T11:12:43","date_gmt":"2023-04-12T03:12:43","guid":{"rendered":"http:\/\/123.57.164.21\/?p=10654"},"modified":"2023-04-12T11:12:43","modified_gmt":"2023-04-12T03:12:43","slug":"node-js-%e5%a6%82%e4%bd%95%e5%a4%84%e7%90%86-es6-%e6%a8%a1%e5%9d%97","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=10654","title":{"rendered":"Node.js \u5982\u4f55\u5904\u7406 ES6 \u6a21\u5757"},"content":{"rendered":"\n<p>\u5b66\u4e60 JavaScript \u8bed\u8a00\uff0c\u4f60\u4f1a\u53d1\u73b0\u5b83\u6709\u4e24\u79cd\u683c\u5f0f\u7684\u6a21\u5757\u3002<\/p>\n\n\n\n<p>\u4e00\u79cd\u662f ES6 \u6a21\u5757\uff0c\u7b80\u79f0 ESM\uff1b\u53e6\u4e00\u79cd\u662f Node.js \u4e13\u7528\u7684 CommonJS \u6a21\u5757\uff0c\u7b80\u79f0 CJS\u3002\u8fd9\u4e24\u79cd\u6a21\u5757\u4e0d\u517c\u5bb9\u3002<\/p>\n\n\n\n<p>\u5f88\u591a\u4eba\u4f7f\u7528 Node.js\uff0c\u53ea\u4f1a\u7528<code>require()<\/code>\u52a0\u8f7d\u6a21\u5757\uff0c\u9047\u5230 ES6 \u6a21\u5757\u5c31\u4e0d\u77e5\u9053\u8be5\u600e\u4e48\u529e\u3002\u672c\u6587\u5c31\u6765\u8c08\u8c08\uff0cES6 \u6a21\u5757\u5728 Node.js \u91cc\u9762\u600e\u4e48\u4f7f\u7528\u3002<\/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-135-1024x465.png\" alt=\"\" class=\"wp-image-10655\" width=\"420\" height=\"191\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-135-1024x465.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-135-300x136.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-135-768x349.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-135-830x377.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-135-230x105.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-135-350x159.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-135-480x218.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-135.png 1386w\" sizes=\"(max-width: 420px) 100vw, 420px\" \/><\/figure><\/div>\n\n\n\n<p><strong>\u4e00\u3001\u4e24\u79cd\u6a21\u5757\u7684\u5dee\u5f02<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>ES6 \u6a21\u5757\u548c CommonJS \u6a21\u5757\u6709\u5f88\u5927\u7684\u5dee\u5f02\u3002<\/p>\n\n\n\n<p>\u8bed\u6cd5\u4e0a\u9762\uff0cCommonJS \u6a21\u5757\u4f7f\u7528<code>require()<\/code>\u52a0\u8f7d\u548c<code>module.exports<\/code>\u8f93\u51fa\uff0cES6 \u6a21\u5757\u4f7f\u7528<code>import<\/code>\u548c<code>export<\/code>\u3002<\/p>\n\n\n\n<p>\u7528\u6cd5\u4e0a\u9762\uff0c<code>require()<\/code>\u662f\u540c\u6b65\u52a0\u8f7d\uff0c\u540e\u9762\u7684\u4ee3\u7801\u5fc5\u987b\u7b49\u5f85\u8fd9\u4e2a\u547d\u4ee4\u6267\u884c\u5b8c\uff0c\u624d\u4f1a\u6267\u884c\u3002<code>import<\/code>\u547d\u4ee4\u5219\u662f\u5f02\u6b65\u52a0\u8f7d\uff0c\u6216\u8005\u66f4\u51c6\u786e\u5730\u8bf4\uff0cES6 \u6a21\u5757\u6709\u4e00\u4e2a\u72ec\u7acb\u7684\u9759\u6001\u89e3\u6790\u9636\u6bb5\uff0c\u4f9d\u8d56\u5173\u7cfb\u7684\u5206\u6790\u662f\u5728\u90a3\u4e2a\u9636\u6bb5\u5b8c\u6210\u7684\uff0c\u6700\u5e95\u5c42\u7684\u6a21\u5757\u7b2c\u4e00\u4e2a\u6267\u884c\u3002<\/p>\n\n\n\n<p><strong>\u4e8c\u3001Node.js \u7684\u533a\u5206<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>Node.js \u8981\u6c42 ES6 \u6a21\u5757\u91c7\u7528<code>.mjs<\/code>\u540e\u7f00\u6587\u4ef6\u540d\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u53ea\u8981\u811a\u672c\u6587\u4ef6\u91cc\u9762\u4f7f\u7528<code>import<\/code>\u6216\u8005<code>export<\/code>\u547d\u4ee4\uff0c\u90a3\u4e48\u5c31\u5fc5\u987b\u91c7\u7528<code>.mjs<\/code>\u540e\u7f00\u540d\u3002Node.js \u9047\u5230<code>.mjs<\/code>\u6587\u4ef6\uff0c\u5c31\u8ba4\u4e3a\u5b83\u662f ES6 \u6a21\u5757\uff0c\u9ed8\u8ba4\u542f\u7528\u4e25\u683c\u6a21\u5f0f\uff0c\u4e0d\u5fc5\u5728\u6bcf\u4e2a\u6a21\u5757\u6587\u4ef6\u9876\u90e8\u6307\u5b9a<code>\"use strict\"<\/code>\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u4e0d\u5e0c\u671b\u5c06\u540e\u7f00\u540d\u6539\u6210<code>.mjs<\/code>\uff0c\u53ef\u4ee5\u5728\u9879\u76ee\u7684<code>package.json<\/code>\u6587\u4ef6\u4e2d\uff0c\u6307\u5b9a<code>type<\/code>\u5b57\u6bb5\u4e3a<code>module<\/code>\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=\"\">\n{\n   \"type\": \"module\"\n}\n<\/pre>\n\n\n\n<p>\u4e00\u65e6\u8bbe\u7f6e\u4e86\u4ee5\u540e\uff0c\u8be5\u76ee\u5f55\u91cc\u9762\u7684 JS \u811a\u672c\uff0c\u5c31\u88ab\u89e3\u91ca\u7528 ES6 \u6a21\u5757\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=\"\"># \u89e3\u91ca\u6210 ES6 \u6a21\u5757\n$ node my-app.js\n<\/pre>\n\n\n\n<p>\u5982\u679c\u8fd9\u65f6\u8fd8\u8981\u4f7f\u7528 CommonJS \u6a21\u5757\uff0c\u90a3\u4e48\u9700\u8981\u5c06 CommonJS \u811a\u672c\u7684\u540e\u7f00\u540d\u90fd\u6539\u6210<code>.cjs<\/code>\u3002\u5982\u679c\u6ca1\u6709<code>type<\/code>\u5b57\u6bb5\uff0c\u6216\u8005<code>type<\/code>\u5b57\u6bb5\u4e3a<code>commonjs<\/code>\uff0c\u5219<code>.js<\/code>\u811a\u672c\u4f1a\u88ab\u89e3\u91ca\u6210 CommonJS \u6a21\u5757\u3002<\/p>\n\n\n\n<p>\u603b\u7ed3\u4e3a\u4e00\u53e5\u8bdd\uff1a<code>.mjs<\/code>\u6587\u4ef6\u603b\u662f\u4ee5 ES6 \u6a21\u5757\u52a0\u8f7d\uff0c<code>.cjs<\/code>\u6587\u4ef6\u603b\u662f\u4ee5 CommonJS \u6a21\u5757\u52a0\u8f7d\uff0c<code>.js<\/code>\u6587\u4ef6\u7684\u52a0\u8f7d\u53d6\u51b3\u4e8e<code>package.json<\/code>\u91cc\u9762<code>type<\/code>\u5b57\u6bb5\u7684\u8bbe\u7f6e\u3002<\/p>\n\n\n\n<p>\u6ce8\u610f\uff0cES6 \u6a21\u5757\u4e0e CommonJS \u6a21\u5757\u5c3d\u91cf\u4e0d\u8981\u6df7\u7528\u3002<code>require<\/code>\u547d\u4ee4\u4e0d\u80fd\u52a0\u8f7d<code>.mjs<\/code>\u6587\u4ef6\uff0c\u4f1a\u62a5\u9519\uff0c\u53ea\u6709<code>import<\/code>\u547d\u4ee4\u624d\u53ef\u4ee5\u52a0\u8f7d<code>.mjs<\/code>\u6587\u4ef6\u3002\u53cd\u8fc7\u6765\uff0c<code>.mjs<\/code>\u6587\u4ef6\u91cc\u9762\u4e5f\u4e0d\u80fd\u4f7f\u7528<code>require<\/code>\u547d\u4ee4\uff0c\u5fc5\u987b\u4f7f\u7528<code>import<\/code>\u3002<\/p>\n\n\n\n<p><strong>\u4e09\u3001CommonJS \u6a21\u5757\u52a0\u8f7d ES6 \u6a21\u5757<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>CommonJS \u7684<code>require()<\/code>\u547d\u4ee4\u4e0d\u80fd\u52a0\u8f7d ES6 \u6a21\u5757\uff0c\u4f1a\u62a5\u9519\uff0c\u53ea\u80fd\u4f7f\u7528<code>import()<\/code>\u8fd9\u4e2a\u65b9\u6cd5\u52a0\u8f7d\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=\"\">(async () => {\n  await import('.\/my-app.mjs');\n})();\n<\/pre>\n\n\n\n<p>\u4e0a\u9762\u4ee3\u7801\u53ef\u4ee5\u5728 CommonJS \u6a21\u5757\u4e2d\u8fd0\u884c\u3002<\/p>\n\n\n\n<p><code>require()<\/code>\u4e0d\u652f\u6301 ES6 \u6a21\u5757\u7684\u4e00\u4e2a\u539f\u56e0\u662f\uff0c\u5b83\u662f\u540c\u6b65\u52a0\u8f7d\uff0c\u800c ES6 \u6a21\u5757\u5185\u90e8\u53ef\u4ee5\u4f7f\u7528\u9876\u5c42<code>await<\/code>\u547d\u4ee4\uff0c\u5bfc\u81f4\u65e0\u6cd5\u88ab\u540c\u6b65\u52a0\u8f7d\u3002<\/p>\n\n\n\n<p><strong>\u56db\u3001ES6 \u6a21\u5757\u52a0\u8f7d CommonJS \u6a21\u5757<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>ES6 \u6a21\u5757\u7684<code>import<\/code>\u547d\u4ee4\u53ef\u4ee5\u52a0\u8f7d CommonJS \u6a21\u5757\uff0c\u4f46\u662f\u53ea\u80fd\u6574\u4f53\u52a0\u8f7d\uff0c\u4e0d\u80fd\u53ea\u52a0\u8f7d\u5355\u4e00\u7684\u8f93\u51fa\u9879\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=\"\">\n\/\/ \u6b63\u786e\nimport packageMain from 'commonjs-package';\n\n\/\/ \u62a5\u9519\nimport { method } from 'commonjs-package';\n<\/pre>\n\n\n\n<p>\u8fd9\u662f\u56e0\u4e3a ES6 \u6a21\u5757\u9700\u8981\u652f\u6301\u9759\u6001\u4ee3\u7801\u5206\u6790\uff0c\u800c CommonJS \u6a21\u5757\u7684\u8f93\u51fa\u63a5\u53e3\u662f<code>module.exports<\/code>\uff0c\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u65e0\u6cd5\u88ab\u9759\u6001\u5206\u6790\uff0c\u6240\u4ee5\u53ea\u80fd\u6574\u4f53\u52a0\u8f7d\u3002<\/p>\n\n\n\n<p>\u52a0\u8f7d\u5355\u4e00\u7684\u8f93\u51fa\u9879\uff0c\u53ef\u4ee5\u5199\u6210\u4e0b\u9762\u8fd9\u6837\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=\"\">import packageMain from 'commonjs-package';\nconst { method } = packageMain;<\/pre>\n\n\n\n<p><strong>\u4e94\u3001\u540c\u65f6\u652f\u6301\u4e24\u79cd\u683c\u5f0f\u7684\u6a21\u5757<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u4e00\u4e2a\u6a21\u5757\u540c\u65f6\u8981\u652f\u6301 CommonJS \u548c ES6 \u4e24\u79cd\u683c\u5f0f\uff0c\u4e5f\u5f88\u5bb9\u6613\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u539f\u59cb\u6a21\u5757\u662f ES6 \u683c\u5f0f\uff0c\u90a3\u4e48\u9700\u8981\u7ed9\u51fa\u4e00\u4e2a\u6574\u4f53\u8f93\u51fa\u63a5\u53e3\uff0c\u6bd4\u5982<code>export default obj<\/code>\uff0c\u4f7f\u5f97 CommonJS \u53ef\u4ee5\u7528<code>import()<\/code>\u8fdb\u884c\u52a0\u8f7d\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u539f\u59cb\u6a21\u5757\u662f CommonJS \u683c\u5f0f\uff0c\u90a3\u4e48\u53ef\u4ee5\u52a0\u4e00\u4e2a\u5305\u88c5\u5c42\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=\"\">\nimport cjsModule from '..\/index.js';\nexport const foo = cjsModule.foo; <\/pre>\n\n\n\n<p>\u4e0a\u9762\u4ee3\u7801\u5148\u6574\u4f53\u8f93\u5165 CommonJS \u6a21\u5757\uff0c\u7136\u540e\u518d\u6839\u636e\u9700\u8981\u8f93\u51fa\u5177\u540d\u63a5\u53e3\u3002<\/p>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u628a\u8fd9\u4e2a\u6587\u4ef6\u7684\u540e\u7f00\u540d\u6539\u4e3a<code>.mjs<\/code>\uff0c\u6216\u8005\u5c06\u5b83\u653e\u5728\u4e00\u4e2a\u5b50\u76ee\u5f55\uff0c\u518d\u5728\u8fd9\u4e2a\u5b50\u76ee\u5f55\u91cc\u9762\u653e\u4e00\u4e2a\u5355\u72ec\u7684<code>package.json<\/code>\u6587\u4ef6\uff0c\u6307\u660e<code>{ type: \"module\" }<\/code>\u3002<\/p>\n\n\n\n<p>\u53e6\u4e00\u79cd\u505a\u6cd5\u662f\u5728<code>package.json<\/code>\u6587\u4ef6\u7684<code>exports<\/code>\u5b57\u6bb5\uff0c\u6307\u660e\u4e24\u79cd\u683c\u5f0f\u6a21\u5757\u5404\u81ea\u7684\u52a0\u8f7d\u5165\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=\"\">\"exports\"\uff1a{ \n    \"require\": \".\/index.js\"\uff0c\n    \"import\": \".\/esm\/wrapper.js\" \n}\n<\/pre>\n\n\n\n<p>\u4e0a\u9762\u4ee3\u7801\u6307\u5b9a<code>require()<\/code>\u548c<code>import<\/code>\uff0c\u52a0\u8f7d\u8be5\u6a21\u5757\u4f1a\u81ea\u52a8\u5207\u6362\u5230\u4e0d\u4e00\u6837\u7684\u5165\u53e3\u6587\u4ef6\u3002<\/p>\n\n\n\n<p>\u603b\u7ed3\u4e3a\u4e00\u53e5\u8bdd\uff1a.mjs\u6587\u4ef6\u603b\u662f\u4ee5 ES6 \u6a21\u5757\u52a0\u8f7d\uff0c.cjs\u6587\u4ef6\u603b\u662f\u4ee5 CommonJS \u6a21\u5757\u52a0\u8f7d\uff0c.js\u6587\u4ef6\u7684\u52a0\u8f7d\u53d6\u51b3\u4e8epackage.json\u91cc\u9762type\u5b57\u6bb5\u7684\u8bbe\u7f6e\u3002<\/p>\n\n\n\n<p>\u6ce8\u610f\uff0cES6 \u6a21\u5757\u4e0e CommonJS \u6a21\u5757\u5c3d\u91cf\u4e0d\u8981\u6df7\u7528\u3002require\u547d\u4ee4\u4e0d\u80fd\u52a0\u8f7d.mjs\u6587\u4ef6\uff0c\u4f1a\u62a5\u9519\uff0c\u53ea\u6709import\u547d\u4ee4\u624d\u53ef\u4ee5\u52a0\u8f7d.mjs\u6587\u4ef6\u3002\u53cd\u8fc7\u6765\uff0c.mjs\u6587\u4ef6\u91cc\u9762\u4e5f\u4e0d\u80fd\u4f7f\u7528require\u547d\u4ee4\uff0c\u5fc5\u987b\u4f7f\u7528import\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b66\u4e60 JavaScript \u8bed\u8a00\uff0c\u4f60\u4f1a\u53d1\u73b0\u5b83\u6709\u4e24\u79cd\u683c\u5f0f\u7684\u6a21\u5757\u3002 \u4e00\u79cd\u662f ES6 \u6a21\u5757\uff0c\u7b80\u79f0 ESM\uff1b\u53e6\u4e00\u79cd\u662f  [&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\/10654"}],"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=10654"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/10654\/revisions"}],"predecessor-version":[{"id":10656,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/10654\/revisions\/10656"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10654"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10654"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10654"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}