{"id":53005,"date":"2024-12-03T11:59:31","date_gmt":"2024-12-03T03:59:31","guid":{"rendered":"https:\/\/fwq.ai\/blog\/53005\/"},"modified":"2024-12-03T11:59:31","modified_gmt":"2024-12-03T03:59:31","slug":"nodejs%e6%a8%a1%e5%9d%97%e3%80%90%e7%ae%80%e5%8d%95%e8%af%b4%e6%98%8e%e3%80%91","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/53005\/","title":{"rendered":"NodeJS\u6a21\u5757\u3010\u7b80\u5355\u8bf4\u660e\u3011"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>NodeJS\u6a21\u5757\u3010\u7b80\u5355\u8bf4\u660e\u3011<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u73cd\u60dc\u65f6\u95f4\uff0c\u52e4\u594b\u5b66\u4e60\uff01\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aNodeJS\u6a21\u5757\u3010\u7b80\u5355\u8bf4\u660e\u3011\u300b<\/span>\uff0c\u6b63\u6587\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u6216\u8005\u662f\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u6709\u7591\u95ee\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u6211\uff01\u540e\u9762\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u5185\u5bb9\u7684\uff0c\u5e0c\u671b\u90fd\u80fd\u5e2e\u5230\u6b63\u5728\u5b66\u4e60\u7684\u5927\u5bb6\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241125\/173252235267443170c9a0d.jpg\" class=\"aligncenter\" title=\"NodeJS\u6a21\u5757\u3010\u7b80\u5355\u8bf4\u660e\u3011\u63d2\u56fe\" alt=\"NodeJS\u6a21\u5757\u3010\u7b80\u5355\u8bf4\u660e\u3011\u63d2\u56fe\" \/><\/p>\n<h2> \u4ec0\u4e48\u662f\u6a21\u5757\uff1f <\/h2>\n<p>\u5047\u8bbe\u60a8\u7684 nodejs \u9879\u76ee\u4e2d\u6709\u4e00\u4e2a index.js \u6587\u4ef6\uff0c\u60a8\u5728\u5176\u4e2d\u4f7f\u7528\u4e86\u4e94\u4e2a\u51fd\u6570\u3002\u4f46\u662f\uff0c\u5176\u4e2d\u4e24\u4e2a\u51fd\u6570\u4e5f\u53ef\u80fd\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u4f7f\u7528\u3002\u56e0\u6b64\uff0c\u60a8\u5728\u9879\u76ee\u4e2d\u521b\u5efa\u4e86\u4e09\u4e2a\u6587\u4ef6\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u6587\u4ef6\uff1a<\/p>\n<ul>\n<li>index.js<\/li>\n<li>dependency1.js<\/li>\n<li>dependency2.js<\/li>\n<\/ul>\n<p>\u8fd9\u91cc\uff0c\u6bcf\u4e2ajavascript\u6587\u4ef6\u90fd\u662f\u4e00\u4e2a\u6a21\u5757\uff0c\u5bfc\u51fa\u7c7b\/\u51fd\u6570\u548c\u5bfc\u5165\u5b83\u4eec\u7684\u65b9\u5f0f\u57fa\u672c\u4e0a\u662f\u6a21\u5757\u7cfb\u7edf\u3002\u6a21\u5757\u7cfb\u7edf\u5141\u8bb8\u6211\u4eec\u62c6\u5206\u548c\u5305\u542b\u4ee3\u7801\uff0c\u5e76\u5728\u9700\u8981\u65f6\u5bfc\u5165\u6211\u4eec\u6216\u5176\u4ed6\u5f00\u53d1\u4eba\u5458\u7f16\u5199\u7684\u4ee3\u7801\u3002<\/p>\n<p>\u8fd9\u4e9b\u6a21\u5757\u4e0d\u4ec5\u4ec5\u662f\u9879\u76ee\u4e2d\u5b58\u5728\u7684 javascript \u6587\u4ef6\uff1b\u5b83\u4eec\u4e5f\u53ef\u4ee5\u662f\u60a8\u5728\u9879\u76ee\u4e2d\u4f5c\u4e3a\u4f9d\u8d56\u9879\u5b89\u88c5\u7684\u4efb\u4f55\u5916\u90e8\u5305\u3002\u53e6\u5916\uff0cnodejs \u6709\u4e00\u4e9b\u5185\u7f6e\u6a21\u5757\uff0c\u5982 http\u3001fs \u7b49\uff0c\u8fd9\u4e9b\u6a21\u5757\u53ef\u4ee5\u968f\u5b89\u88c5\u4e00\u8d77\u4f7f\u7528\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u4e0d\u6dfb\u52a0\u4efb\u4f55\u5916\u90e8\u4f9d\u8d56\u9879\u7684\u60c5\u51b5\u4e0b\u5bfc\u5165\u3002<\/p>\n<h2> \u5982\u4f55\u5bfc\u51fa\u6216\u5bfc\u5165\u6a21\u5757\uff1f <\/h2>\n<p>node.js \u4e2d\u4f7f\u7528\u4e86\u4e24\u4e2a\u6a21\u5757\u7cfb\u7edf\u3002 <\/p>\n<ol>\n<li>commonjs (cjs)<\/li>\n<li>ecmascript \u6a21\u5757 (esm)<\/li>\n<\/ol>\n<p>\u60a8\u5df2\u7ecf\u9605\u8bfb\u4e86\u6211\u4e4b\u524d\u535a\u5ba2\u4e2d\u7684\u53a8\u623f-\u53a8\u5e08-\u670d\u52a1\u5458\u793a\u4f8b\uff1b\u540c\u6837\uff0c\u5982\u679c\u6211\u4eec\u5c06\u6a21\u5757\u4e0e\u6b64\u8fdb\u884c\u6bd4\u8f83\uff0c\u60f3\u8c61 commonjs \u662f\u4e00\u672c\u53e4\u8001\u7684\u5927\u98df\u8c31\u4e66\uff0c\u6211\u4eec\u9700\u8981\u641c\u7d22\u548c\u67e5\u627e\u98df\u8c31\uff0c\u800c esm \u662f\u4e00\u4e2a\u65b0\u7684\u8001\u5f0f\u6570\u5b57\u5e94\u7528\u7a0b\u5e8f\u6765\u67e5\u770b\u98df\u8c31\u3002<\/p>\n<p><strong>commonjs (cjs)<\/strong><\/p>\n<ul>\n<li>\u65e7\u7684\u6a21\u5757\u7cfb\u7edf\u3002<\/li>\n<li>\u6a21\u5757\u4ee5\u7b80\u5355\u7684\u683c\u5f0f\u7f16\u5199\uff0c\u4f8b\u5982\uff1a <\/li>\n<\/ul>\n<pre>\/\/ export\nmodule.exports = function sayhello() {\n  console.log(\"hello world!\");\n};\n\n\/\/ import\nconst gethello = require(\".\/hello-script.js\");\nsayhello(); \/\/ \"hello world!\"\n<\/pre>\n<ul>\n<li>\u540c\u6b65\uff1a\u4f9d\u6b21\u52a0\u8f7d\u6a21\u5757\u3002<\/li>\n<li>\u4ec5\u5728 node.js \u4e2d\u5de5\u4f5c\uff08\u4e0d\u80fd\u76f4\u63a5\u5728\u6d4f\u89c8\u5668\u4e2d\u5de5\u4f5c\uff09\u3002<\/li>\n<\/ul>\n<p><strong>ecmascript \u6a21\u5757 (esm)<\/strong><\/p>\n<ul>\n<li>\u73b0\u4ee3\u6a21\u5757\u7cfb\u7edf\u3002<\/li>\n<li>\u6a21\u5757\u73b0\u5728\u66f4\u52a0\u7ed3\u6784\u5316\uff1a <\/li>\n<\/ul>\n<pre>\/\/ export\nexport function sayhello() {\n  console.log(\"hello world!\");\n}\n\n\/\/ import\nimport { sayhello } from \".\/hello-script.js\";\nsayhello();\n<\/pre>\n<ul>\n<li>\u5f02\u6b65\uff1a\u540c\u65f6\u52a0\u8f7d\u591a\u4e2a\u6a21\u5757\u3002<\/li>\n<li>\u5728\u6d4f\u89c8\u5668\u548c node.js \u4e2d\u539f\u751f\u5de5\u4f5c\u3002<\/li>\n<\/ul>\n<p><strong>\u8bed\u6cd5\u4e0a\u7684\u4e3b\u8981\u533a\u522b\uff1a<\/strong><br \/> cjs\uff1a module.exports \/ require<br \/> esm\uff1a\u5bfc\u51fa\/\u5bfc\u5165<\/p>\n<h2> \u5982\u4f55\u5728\u6211\u7684\u9879\u76ee\u4e2d\u914d\u7f6e cjs \u6216 esm\uff1f <\/h2>\n<ul>\n<li>\u6253\u5f00\u9879\u76ee\u7684package.json\u3002<\/li>\n<li>\u6dfb\u52a0\uff1a <\/li>\n<\/ul>\n<pre>{\n  type: \"module\";\n}\n<\/pre>\n<ul>\n<li>\u8fd9\u544a\u8bc9 nodejs \u5c06 .js \u89e3\u91ca\u4e3a esm\u3002\u5982\u679c\u6211\u4eec\u4e0d\u6dfb\u52a0\u8fd9\u4e2a\uff0cnodejs \u5c06\u9ed8\u8ba4\u5c06\u5176\u89e3\u91ca\u4e3a commonjs\u3002<\/li>\n<\/ul>\n<h2> typescript \u4e2d\u7684\u6a21\u5757\uff1a <\/h2>\n<p>\u6709\u65f6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 ecmascript \u6a21\u5757\uff0c\u4f46\u60a8\u5bfc\u5165\u7684\u65e7\u5305\u662f\u7528 commonjs \u7f16\u5199\u7684\u3002\u4e3a\u4e86\u5904\u7406\u8fd9\u4e9b\u60c5\u51b5\uff0c\u6211\u4eec\u6709\u65f6\u4f1a\u786e\u4fdd\u4ece typescript \u6587\u4ef6\u751f\u6210\u7684\u8f93\u51fa javascript \u4ee3\u7801\u91c7\u7528\u901a\u7528\u683c\u5f0f\uff0c\u5373\u4f7f\u6211\u4eec\u5df2\u7ecf\u4ee5 esm \u683c\u5f0f\u7f16\u5199\u4e86 typescript \u6587\u4ef6\u3002<\/p>\n<p>\u4e3a\u6b64\uff0c\u6211\u4eec\u5728\u9879\u76ee\u7684 tsconfig.json \u4e2d\u6dfb\u52a0\u7f16\u8bd1\u5668\u9009\u9879\uff1a<\/p>\n<pre>\"compileroptions\": {\n  \"module\": \"commonjs\",\n  \"target\": \"es6\"\n}\n<\/pre>\n<p><strong>\u7136\u540e\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1a<\/strong><br \/> module: &#8220;commonjs&#8221;\uff1a\u4f7f\u7528 commonjs \u6a21\u5757\u7cfb\u7edf\u8f93\u51fa javascript\uff0c\u8be5\u7cfb\u7edf\u4f7f\u7528 require \u548c module.exports\u3002<br \/> target: &#8220;es6&#8243;\uff1a\u786e\u4fdd\u8f93\u51fa javascript \u4f7f\u7528 es6 \u8bed\u6cd5\u548c let\u3001const \u548c\u7bad\u5934\u51fd\u6570\u7b49\u529f\u80fd\u3002<\/p>\n<p>\u8f93\u5165\u6253\u5b57\u7a3f\u4ee3\u7801\uff1a<\/p>\n<pre>import { readfile } from \"fs\";\n\nconst sayhello = () =&gt; {\n  console.log(\"hello world!\");\n};\n\nexport { sayhello };\n<\/pre>\n<p>\u8f93\u51fajavascript\u4ee3\u7801\uff1a<\/p>\n<pre>\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.SayHello = void 0;\nconst fs_1 = require(\"fs\");\nconst SayHello = () =&gt; {\n    console.log(\"Hello World!\");\n};\nexports.SayHello = SayHello;\n<\/pre>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aNodeJS\u6a21\u5757\u3010\u7b80\u5355\u8bf4\u660e\u3011\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664    <\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u8fde\u63a5\u4e24\u7ec4\u70b9\u7684\u6700\u4f4e\u6210\u672c\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>NodeJS\u6a21\u5757\u3010\u7b80\u5355\u8bf4\u660e\u3011 \u6536\u85cf \u73cd\u60dc\u65f6\u95f4\uff0c\u52e4\u594b\u5b66\u4e60\uff01\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765\u300aNodeJS\u6a21\u5757\u3010\u7b80\u5355\u8bf4\u660e\u3011\u300b\uff0c\u6b63\u6587\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230\u7b49\u7b49\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u6587\u7ae0\uff0c\u6216\u8005\u662f\u5bf9\u6587\u7ae0\u6709\u7591\u95ee\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u6211\uff01\u540e\u9762\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u5185\u5bb9\u7684\uff0c\u5e0c\u671b\u90fd\u80fd\u5e2e\u5230\u6b63\u5728\u5b66\u4e60\u7684\u5927\u5bb6\uff01 \u4ec0\u4e48\u662f\u6a21\u5757\uff1f \u5047\u8bbe\u60a8\u7684 nodejs \u9879\u76ee\u4e2d\u6709\u4e00\u4e2a index.js \u6587\u4ef6\uff0c\u60a8\u5728\u5176\u4e2d\u4f7f\u7528\u4e86\u4e94\u4e2a\u51fd\u6570\u3002\u4f46\u662f\uff0c\u5176\u4e2d\u4e24\u4e2a\u51fd\u6570\u4e5f\u53ef\u80fd\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u4f7f\u7528\u3002\u56e0\u6b64\uff0c\u60a8\u5728\u9879\u76ee\u4e2d\u521b\u5efa\u4e86\u4e09\u4e2a\u6587\u4ef6\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u6587\u4ef6\uff1a index.js dependency1.js dependency2.js \u8fd9\u91cc\uff0c\u6bcf\u4e2ajavascript\u6587\u4ef6\u90fd\u662f\u4e00\u4e2a\u6a21\u5757\uff0c\u5bfc\u51fa\u7c7b\/\u51fd\u6570\u548c\u5bfc\u5165\u5b83\u4eec\u7684\u65b9\u5f0f\u57fa\u672c\u4e0a\u662f\u6a21\u5757\u7cfb\u7edf\u3002\u6a21\u5757\u7cfb\u7edf\u5141\u8bb8\u6211\u4eec\u62c6\u5206\u548c\u5305\u542b\u4ee3\u7801\uff0c\u5e76\u5728\u9700\u8981\u65f6\u5bfc\u5165\u6211\u4eec\u6216\u5176\u4ed6\u5f00\u53d1\u4eba\u5458\u7f16\u5199\u7684\u4ee3\u7801\u3002 \u8fd9\u4e9b\u6a21\u5757\u4e0d\u4ec5\u4ec5\u662f\u9879\u76ee\u4e2d\u5b58\u5728\u7684 javascript \u6587\u4ef6\uff1b\u5b83\u4eec\u4e5f\u53ef\u4ee5\u662f\u60a8\u5728\u9879\u76ee\u4e2d\u4f5c\u4e3a\u4f9d\u8d56\u9879\u5b89\u88c5\u7684\u4efb\u4f55\u5916\u90e8\u5305\u3002\u53e6\u5916\uff0cnodejs \u6709\u4e00\u4e9b\u5185\u7f6e\u6a21\u5757\uff0c\u5982 http\u3001fs \u7b49\uff0c\u8fd9\u4e9b\u6a21\u5757\u53ef\u4ee5\u968f\u5b89\u88c5\u4e00\u8d77\u4f7f\u7528\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u4e0d\u6dfb\u52a0\u4efb\u4f55\u5916\u90e8\u4f9d\u8d56\u9879\u7684\u60c5\u51b5\u4e0b\u5bfc\u5165\u3002 \u5982\u4f55\u5bfc\u51fa\u6216\u5bfc\u5165\u6a21\u5757\uff1f node.js \u4e2d\u4f7f\u7528\u4e86\u4e24\u4e2a\u6a21\u5757\u7cfb\u7edf\u3002 commonjs (cjs) ecmascript \u6a21\u5757 (esm) \u60a8\u5df2\u7ecf\u9605\u8bfb\u4e86\u6211\u4e4b\u524d\u535a\u5ba2\u4e2d\u7684\u53a8\u623f-\u53a8\u5e08-\u670d\u52a1\u5458\u793a\u4f8b\uff1b\u540c\u6837\uff0c\u5982\u679c\u6211\u4eec\u5c06\u6a21\u5757\u4e0e\u6b64\u8fdb\u884c\u6bd4\u8f83\uff0c\u60f3\u8c61 commonjs \u662f\u4e00\u672c\u53e4\u8001\u7684\u5927\u98df\u8c31\u4e66\uff0c\u6211\u4eec\u9700\u8981\u641c\u7d22\u548c\u67e5\u627e\u98df\u8c31\uff0c\u800c esm \u662f\u4e00\u4e2a\u65b0\u7684\u8001\u5f0f\u6570\u5b57\u5e94\u7528\u7a0b\u5e8f\u6765\u67e5\u770b\u98df\u8c31\u3002 commonjs (cjs) \u65e7\u7684\u6a21\u5757\u7cfb\u7edf\u3002 \u6a21\u5757\u4ee5\u7b80\u5355\u7684\u683c\u5f0f\u7f16\u5199\uff0c\u4f8b\u5982\uff1a \/\/ export module.exports = function sayhello() { console.log(&#8220;hello world!&#8221;); }; \/\/ import const gethello = require(&#8220;.\/hello-script.js&#8221;); sayhello(); \/\/ &#8220;hello [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-53005","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53005","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=53005"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53005\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=53005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=53005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=53005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}