{"id":10006435,"date":"2022-09-08T08:13:49","date_gmt":"2022-09-08T00:13:49","guid":{"rendered":"https:\/\/ityet.com\/?p=10006435"},"modified":"2022-09-08T08:13:49","modified_gmt":"2022-09-08T00:13:49","slug":"commonjs%e6%a8%a1%e5%9d%97-%e5%92%8c-ecmascript%e6%a8%a1%e5%9d%97%ef%bc%88%e6%9d%a5%e8%87%aa%e5%ae%98%e6%96%b9%e6%96%87%e6%a1%a3%ef%bc%89","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006435","title":{"rendered":"CommonJS\u6a21\u5757 \u548c ECMAScript\u6a21\u5757\uff08\u6765\u81ea\u5b98\u65b9\u6587\u6863\uff09"},"content":{"rendered":"<blockquote>\n<p>CommonJS\u6a21\u5757(\u4e0b\u9762\u7b80\u79f0 cjs) \u662f\u4e3aNode.js\u6253\u5305JavaScript\u4ee3\u7801\u7684\u539f\u59cb\u65b9\u6cd5\u3002\u4f7f\u7528 require \u548c exports\uff08module.exports\u7684\u7b80\u5199\uff09 \u8bed\u53e5\u5b9a\u4e49\u6a21\u5757\u3002 \u53c2\u89c1 <a href=\"https:\/\/link.segmentfault.com\/?enc=wNCBYXIOUbZgZYln58otPg%3D%3D.QQsAMqm3T2%2B9yy7zc1IA7OlryeXg12HOhXKQbYOPU0ElBqIcoi967SkvhJH5Qp25xXMUDGUWaFS%2FwKicGdA8Tg%3D%3D\" rel=\"nofollow\">nodejs cjs<\/a><\/p>\n<p>ECMAScript\u6a21\u5757(\u4e0b\u9762\u7b80\u79f0 esm)\u662fecma262\u6807\u51c6\u4e0b\u5c01\u88c5\u7684JavaScript\u4ee3\u7801\u91cd\u7528\u7684\u5b98\u65b9\u6807\u51c6\u683c\u5f0f\u3002\u4f7f\u7528 import \u548cexport \u8bed\u53e5\u5b9a\u4e49\u6a21\u5757\u3002\u53c2\u89c1<a href=\"https:\/\/link.segmentfault.com\/?enc=R46ZiuV7mbOZ5tA0c%2B7%2B1Q%3D%3D.8%2Fzvxs33NX3mo1xehpY7lcESpmtgED8Mt3AKBIWvv5q3Dcg2te9RoWPxV3VhYdUXv5bfEiw%2FzyelGBe1MGV2zA%3D%3D\" rel=\"nofollow\">node  esm<\/a><\/p>\n<\/blockquote>\n<p>-cjs\u53ea\u6709\u5728node.js\u73af\u5883\u4f7f\u7528\u3002<br \/>-esm\u5728node.js\u548c\u6d4f\u89c8\u5668\u73af\u5883\u90fd\u53ef\u4ee5\u4f7f\u7528<\/p>\n<h3>1\u3001commonjs<\/h3>\n<p>\u5728node.js\u4e2d\uff0c\u6bcf\u4e2a\u6587\u4ef6\u90fd\u88ab\u89c6\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684\u6a21\u5757\u3002\u6a21\u5757\u7684\u5c40\u90e8\u53d8\u91cf\u662f\u79c1\u6709\u7684\uff0c\u53ea\u6709exports\u51fa\u53bb\u7684\u53d8\u91cf\uff0c\u624d\u80fd\u88ab\u5916\u754c\u8bbf\u95ee\u3002<\/p>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cnode.js\u4f1a\u5c06\u4ee5\u4e0b\u60c5\u5f62\u89c6\u4e3a  cjs\u6a21\u5757:<\/p>\n<ul>\n<li>\u6269\u5c55\u540d\u4e3a.cjs\u7684\u6587\u4ef6\uff1b<\/li>\n<li>\u6269\u5c55\u540d\u4e3a.js\u7684\u6587\u4ef6\uff0c\u4e14\u79bb\u81ea\u5df1\u6700\u8fd1\u7684package.json\u6587\u4ef6\u5305\u542b\u4e00\u4e2a\u9876\u7ea7\u5b57\u6bb5\u201ctype\u201d\uff0c\u5176\u503c\u4e3a\u201ccommonjs\u201d\uff1b<\/li>\n<li>\u6269\u5c55\u540d\u4e3a.js\u7684\u6587\u4ef6\uff0c\u4e14\u79bb\u81ea\u5df1\u6700\u8fd1\u7684package.json\u6587\u4ef6\u4e0d\u5305\u542b\u4e00\u4e2a\u9876\u7ea7\u5b57\u6bb5\u201ctype\u201d(\u5efa\u8bae\u660e\u786e\u6307\u5b9a type\u503c\uff0c\u800c\u4e0d\u662f\u4e0d\u5b9a\u4e49)\uff1b<\/li>\n<li>\u6269\u5c55\u540d\u4e0d\u4e3a.mjs, .cjs, .json, .node, .js\u7684\u6587\u4ef6\uff0c\u4e14\u79bb\u81ea\u5df1\u6700\u8fd1\u7684package.json\u6587\u4ef6\u5305\u542b\u4e00\u4e2a\u9876\u7ea7\u5b57\u6bb5\u201ctype\u201d\uff0c\u5176\u503c\u4e3a\u201cmodule\u201d\uff0c\u4f46\u662f\u8fd9\u4e9b\u6587\u4ef6\u901a\u8fc7require\u5f15\u5165\u3002<\/li>\n<\/ul>\n<p>\u8c03\u7528require\uff08\uff09\u65f6\uff0c\u59cb\u7ec8\u4f7f\u7528cjs\u6a21\u5757\u52a0\u8f7d\u5668\uff0c<br \/>require\u91c7\u7528\u540c\u6b65\u65b9\u5f0f\u52a0\u8f7d\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u7684\u4efb\u610f\u4f4d\u7f6e\u4f7f\u7528\u3002<br \/>\u7531\u4e8erequire\uff08\uff09\u7684\u540c\u6b65\u7279\u6027\uff0c\u65e0\u6cd5\u4f7f\u7528\u5b83\u52a0\u8f7dECMAScript\u6a21\u5757\u6587\u4ef6\u3002\u5c1d\u8bd5\u8fd9\u6837\u505a\u5c06\u629b\u51faERR_REQUIRE_ESM\u9519\u8bef\u3002\u8bf7\u6539\u7528import\uff08\uff09\u3002<\/p>\n<p>\u8981\u83b7\u53d6\u8c03\u7528require\uff08\uff09\u65f6\u5c06\u52a0\u8f7d\u7684\u786e\u5207\u6587\u4ef6\u540d\uff0c\u8bf7\u4f7f\u7528require.resolve\uff08\uff09\u51fd\u6570\u3002<\/p>\n<p>1)\u3001\u5bf9\u4e8e\u4e00\u4e2a\u6587\u4ef6\uff0c\u5bfc\u51fa\u7684\u662f\u5bf9\u8c61\u7684\u5f15\u7528\u3002\u5982\u679c\u5185\u90e8\u7684\u5c5e\u6027\u53d8\u5316\u4e86\uff0c\u5916\u90e8\u4e5f\u4f1a\u53d8\u5316\u3002<br \/>2)\u3001\u5bf9\u4e8e\u518d\u6b21\u5f15\u5165\u540c\u4e00\u4e2a\u6587\u4ef6\uff0c\u5982\u679c\u6587\u4ef6\u540d\u4e00\u6a21\u4e00\u6837\uff0c\u5219\u4ece\u7f13\u5b58\u91cc\u9762\u53d6<br \/>3)\u3001\u8fd8\u6709\u79cd\u65b9\u5f0f\uff0c\u8981\u4f7f\u6a21\u5757\u591a\u6b21\u6267\u884c\u4ee3\u7801\uff0c\u8bf7\u5bfc\u51fa\u51fd\u6570\u5e76\u8c03\u7528\u8be5\u51fd\u6570\u3002<\/p>\n<p>\u5bfc\u51fa\u4e00\u4e2a\u539f\u59cb\u503c<\/p>\n<pre><code>\/\/child.js\nvar counter = 3;\nfunction incCounter() {\n  counter++;\n}\nmodule.exports = {\n  counter: counter,\n  incCounter: incCounter,\n};\n\n\/\/parent.js\nlet child = require('.\/child.js')\nconsole.log(child.counter);  \/\/ 3\nchild.incCounter();\nconsole.log(child.counter); \/\/ 3\u6ca1\u53d8<\/code><\/pre>\n<p>\u5bfc\u51fa\u4e00\u4e2a\u5bf9\u8c61<\/p>\n<pre><code>\/\/child.js\nvar obj = {\n    counter:3\n};\nfunction incCounter() {\n    obj.counter++;\n}\nmodule.exports = {\n  obj: obj,\n  incCounter: incCounter,\n};\n\/\/parent.js\nlet child = require('.\/child.js')\nconsole.log(child.obj.counter);  \/\/ 3\ntest.incCounter();\nconsole.log(child.obj.counter); \/\/ 4 \u53d8\u4e86<\/code><\/pre>\n<p>\u5bfc\u51fa\u4e00\u4e2a\u5bf9\u8c61\uff0c\u591a\u4e2a\u6587\u4ef6\uff0c\u4f1a\u4ece\u7f13\u5b58\u91cc\u83b7\u53d6<\/p>\n<pre><code>\/\/child.js\nvar obj = {\n    counter:3\n};\nfunction incCounter() {\n    obj.counter++;\n}\nmodule.exports = {\n  obj: obj,\n  incCounter: incCounter,\n};\n\/\/parent.js\nlet child = require('.\/child.js')\nlet child1 = require('.\/child.js')\nconsole.log(child.obj.counter);  \/\/ 3\ntest.incCounter();\nconsole.log(child.obj.counter); \/\/ 4 \u53d8\u4e86\nconsole.log(child1.obj.counter); \/\/ 4 \u53d8\u4e86<\/code><\/pre>\n<p>\u5982\u679crequire\u52a0\u8f7d\u7684\u662f\u4e2a\u6587\u4ef6\u5939\uff0c\u90a3\u4e48node.js\u9ed8\u8ba4\u4f1a\u5c1d\u8bd5\u52a0\u8f7d index.js \u6216\u8005 index.node<\/p>\n<h4>\u52a8\u6001\u5bfc\u5165<\/h4>\n<p>import() \u65e2\u652f\u6301cjs\uff0c\u53c8\u652f\u6301 esm<\/p>\n<pre><code>import('.\/lib.js').then((res)=&gt;{\n    \/\/res.default  \n}) <\/code><\/pre>\n<h3>2\u3001es module<\/h3>\n<p>\u8c03\u7528 import\uff08\uff09\u65f6\uff0c\u59cb\u7ec8\u4f7f\u7528esm\u6a21\u5757\u52a0\u8f7d\u5668\uff0c<\/p>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cnode.js\u4f1a\u5c06\u4ee5\u4e0b\u60c5\u5f62\u89c6\u4e3a  esm \u6a21\u5757:<br \/>\u6269\u5c55\u540d\u4e3a .mjs \u7684\u6587\u4ef6\uff1b<br \/>\u6269\u5c55\u540d\u4e3a.js\u7684\u6587\u4ef6\uff0c\u4e14\u79bb\u81ea\u5df1\u6700\u8fd1\u7684package.json\u6587\u4ef6\u5305\u542b\u4e00\u4e2a\u9876\u7ea7\u5b57\u6bb5\u201ctype\u201d\uff0c\u5176\u503c\u4e3a\u201cmodule\u201d\uff1b<\/p>\n<p>1)\u3001\u5bf9\u4e8e\u4e00\u4e2a\u6587\u4ef6\uff0c\u5bfc\u51fa\u7684\u662f\u5bf9\u8c61\u7684\u5f15\u7528\u3002\u5982\u679c\u5185\u90e8\u7684\u5c5e\u6027\u53d8\u5316\u4e86\uff0c\u5916\u90e8\u4e5f\u4f1a\u53d8\u5316\u3002<br \/>2)\u3001\u5bf9\u4e8e\u518d\u6b21\u5f15\u5165\u540c\u4e00\u4e2a\u6587\u4ef6\uff0c\u5982\u679c\u6587\u4ef6\u540d\u4e00\u6a21\u4e00\u6837\uff0c\u5219\u4ece\u7f13\u5b58\u91cc\u9762\u53d6\u3002\u5982\u679c\u6587\u4ef6\u540d\u52a0\u4e86query\uff0c\u5219\u91cd\u65b0\u52a0\u8f7d\u6587\u4ef6\uff0c\u4e0d\u4f1a\u7f13\u5b58\u91cc\u53d6<br \/>3)\u3001\u8fd8\u6709\u79cd\u65b9\u5f0f\uff0c\u8981\u4f7f\u6a21\u5757\u591a\u6b21\u6267\u884c\u4ee3\u7801\uff0c\u8bf7\u5bfc\u51fa\u51fd\u6570\u5e76\u8c03\u7528\u8be5\u51fd\u6570\u3002<\/p>\n<p>\u5355\u6b21\u5bfc\u5165\uff0c\u5bfc\u5165\u57fa\u672c\u7c7b\u578b<\/p>\n<pre><code>\/\/child.mjs\nvar counter = 3;\nfunction incCounter() {\n  counter++;\n}\nexport default {\n  counter: counter,\n  incCounter: incCounter,\n};\n\n\/\/index.mjs\nimport child from &quot;.\/lib.mjs&quot;;\nconsole.log(child.counter); \/\/ 3\nchild.incCounter();\nconsole.log(child.counter); \/\/ 3\u6ca1\u53d8<\/code><\/pre>\n<p>\u591a\u6b21\u5bfc\u5165\uff0c\u5bfc\u5165\u5f15\u7528\u7c7b\u578b<\/p>\n<pre><code>\/\/child.mjs\nvar obj = {\n  counter: 3,\n};\nfunction incCounter() {\n  obj.counter++;\n}\nexport default {\n  obj: obj,\n  incCounter: incCounter,\n};\n\n\/\/index.mjs\nimport child from &quot;.\/lib.mjs&quot;;\nimport child1 from &quot;.\/lib.mjs&quot;;  \/\/\u4ece\u7f13\u5b58\u91cc\u9762\u53d6\nconsole.log(child.obj.counter); \/\/ 3\nchild.incCounter();\nconsole.log(child.obj.counter); \/\/ 4\u53d8\u4e86\n\nconsole.log(child1.obj.counter); \/\/ 4\u53d8\u4e86\uff0c\u62ff\u7684\u7f13\u5b58\u91cc\u7684<\/code><\/pre>\n<p>\u591a\u6b21\u5bfc\u5165\uff0c\u907f\u514d\u7f13\u5b58<\/p>\n<pre><code>\/\/child.mjs\nvar obj = {\n  counter: 3,\n};\nfunction incCounter() {\n  obj.counter++;\n}\nexport default {\n  obj: obj,\n  incCounter: incCounter,\n};\n\n\/\/index.mjs\nimport child from &quot;.\/lib.mjs?time=1&quot;;\nimport child1 from &quot;.\/lib.mjs?time=2&quot;;  \/\/\u589e\u52a0query\u907f\u514d\u7f13\u5b58\nconsole.log(child.obj.counter); \/\/ 3\nchild.incCounter();\nconsole.log(child.obj.counter); \/\/ 4\u53d8\u4e86\nconsole.log(child1.obj.counter); \/\/ 3\u6ca1\u53d8\uff0c\u91cd\u65b0\u52a0\u8f7d\u7684\u6587\u4ef6<\/code><\/pre>\n<h4>import.meta.url<\/h4>\n<p>\u5f53\u524d\u6587\u4ef6\u6587\u4ef6\u6a21\u5757\u7684 url\u5730\u5740\u3002<\/p>\n<pre><code>import { readFileSync } from 'node:fs';\nconst buffer = readFileSync(new URL('.\/data.proto', import.meta.url));<\/code><\/pre>\n<p>import.meta.resolve  \u5b9e\u9a8c\u529f\u80fd<\/p>\n<pre><code>await import.meta.resolve('.\/dep', import.meta.url);\nconst dependencyAsset = await import.meta.resolve('component-lib\/asset.css');<\/code><\/pre>\n<h4>\u5728esm\u4e2d\u4f7f\u7528 require<\/h4>\n<pre><code>import { createRequire } from 'node:module';  \nconst require = createRequire(import.meta.url);\n\n\/\/ sibling-module.js is a CommonJS module.\nconst siblingModule = require('.\/sibling-module');<\/code><\/pre>\n<p>\u6ce8\u610f\uff1a \u4ecenode.js v16\u4ee5\u540e\uff0c\u4f7f\u7528node\u5185\u7f6e\u6838\u5fc3\u6a21\u5757\u90fd\u91c7\u7528node:xx\uff0c \u6bd4\u5982 node:fs\uff0c\u5b83\u548cfs\u7684\u533a\u522b\u662f\uff0c\u4e0d\u80fd\u88abrequire\u7f13\u5b58\uff0c\u800cfs\u80fd\u88ab\u7f13\u5b58\uff0c\u53ef\u4ee5\u67e5\u770b <a href=\"https:\/\/link.segmentfault.com\/?enc=vuK6se18zMIkCxtUl9EnRg%3D%3D.PX70xQdPD%2BorKoIFIPuYWPOczKtcj7CY%2BFS5Bd3PE5UZXNB3GhcptWellg8UJrU3\" rel=\"nofollow\">v16 changeLog-issue<\/a><\/p>\n<pre><code>const path = require('node:path');  \/\/\u4e0d\u88ab\u7f13\u5b58\nconst path = require('path');    \/\/\u53ef\u4ee5\u88ab\u7f13\u5b58<\/code><\/pre>\n<h3>3\u3001esm\u6a21\u5757\u4e0ecjs\u6a21\u5757\u4e4b\u95f4\u7684\u5dee\u5f02<\/h3>\n<ul>\n<li>esm\u4f7f\u7528import\/export\uff0c \u800ccjs\u4f7f\u7528require\/exports<\/li>\n<li>cjs\u53ef\u4ee5\u4f7f\u7528 __filename \u6216\u8005 __dirname\uff0c \u800cesm\u4e0d\u884c\uff0cesm\u53ea\u80fd\u4f7f\u7528 import.meta.url<\/li>\n<li>esm\u4e0d\u652f\u6301\u672c\u673a\u6a21\u5757\u3002 ith module.createRequire() or process.dlopen.<\/li>\n<li>cjs\u4f7f\u7528 require.resolve\uff0c \u800cesm\u4f7f\u7528new URL(), import.meta.resolve<\/li>\n<li>cjs\u53ef\u4ee5\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u6307\u5b9a\u7684\u8def\u5f84\uff0c\u53bb\u67e5\u627e\u672c\u673a\u4e0a\u5bf9\u5e94\u4f4d\u7f6e\u7684\u6a21\u5757\uff0c\u800cesm\u4e0d\u884c\uff0c<\/li>\n<li>cjs \u662f\u5728\u8fd0\u884c\u65f6\u786e\u5b9a\uff0c\u800cesm\u5219\u5728\u9759\u6001\u7f16\u8bd1\u65f6\u786e\u5b9a\u3002<\/li>\n<li>cjs\u53ef\u4ee5\u540c\u6b65\u6267\u884c\uff0cesm\u4e0d\u884c<\/li>\n<\/ul>\n<p>\u5171\u540c\u70b9\uff1a\u6a21\u5757\u5bfc\u51fa\u7684\u90fd\u662f\u5f15\u7528\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042443391\">https:\/\/segmentfault.com\/a\/1190000042443391<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>CommonJS\u6a21\u5757(\u4e0b\u9762\u7b80\u79f0 cjs) \u662f\u4e3aNode.js\u6253\u5305JavaScript\u4ee3\u7801\u7684\u539f\u59cb\u65b9\u6cd5\u3002\u4f7f\u7528 re [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006435","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006435","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006435"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006435\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006435"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}