{"id":61466,"date":"2023-11-14T10:38:29","date_gmt":"2023-11-14T02:38:29","guid":{"rendered":"https:\/\/docs.pingcode.com\/?p=61466"},"modified":"2023-11-14T10:38:40","modified_gmt":"2023-11-14T02:38:40","slug":"node-%e4%b8%ad-requirenode-path-%e4%b8%8e-requirepath%e7%9a%84%e5%8c%ba%e5%88%ab%e6%98%af%e4%bb%80%e4%b9%88","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/61466.html","title":{"rendered":"node \u4e2d require(node path) \u4e0e require(path)\u7684\u533a\u522b\u662f\u4ec0\u4e48"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote\">\n<p>\u5728 Node.js \u7f16\u7a0b\u4e2d\uff0crequire(&#8216;path&#8217;) \u548c require(&#8216;node:path&#8217;) \u90fd\u7528\u4e8e\u5bfc\u5165 Node.js \u7684\u5185\u7f6e path \u6a21\u5757\uff0c\u4f46\u5b83\u4eec\u4e4b\u95f4\u5b58\u5728\u7ec6\u5fae\u7684\u5dee\u5f02\u3002\u672c\u6587\u5c06\u63a2\u8ba8\u8fd9\u4e24\u79cd\u5bfc\u5165\u65b9\u5f0f\u7684\u533a\u522b\uff0c\u5305\u62ec\u5b83\u4eec\u7684\u89e3\u6790\u673a\u5236\u3001\u6a21\u5757\u7f13\u5b58\u5904\u7406\u3001\u5bf9\u4e0d\u540c Node.js \u7248\u672c\u7684\u517c\u5bb9\u6027\uff0c\u4ee5\u53ca\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u7684\u5e94\u7528\u573a\u666f\u3002\u867d\u7136\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u4e24\u79cd\u5bfc\u5165\u65b9\u5f0f\u53ef\u4e92\u6362\u4f7f\u7528\uff0c\u7406\u89e3\u5b83\u4eec\u7684\u4e0d\u540c\u4e4b\u5904\u5bf9\u4e8e\u7f16\u5199\u66f4\u5065\u58ee\u3001\u53ef\u7ef4\u62a4\u7684 Node.js \u4ee3\u7801\u81f3\u5173\u91cd\u8981\u3002\u672c\u6587\u65e8\u5728\u4e3a Node.js \u5f00\u53d1\u8005\u63d0\u4f9b\u6e05\u6670\u7684\u6307\u5bfc\uff0c\u5e2e\u52a9\u4ed6\u4eec\u5728\u4e0d\u540c\u573a\u666f\u4e0b\u9009\u62e9\u9002\u5f53\u7684\u6a21\u5757\u5bfc\u5165\u65b9\u6cd5\u3002<\/p>\n<\/blockquote>\n\n\n\n<p><strong>1.<\/strong><strong>\u6a21\u5757\u89e3\u6790\u673a\u5236<\/strong><\/p>\n\n\n\n<p>require(&#8216;path&#8217;) \u9075\u5faa Node.js \u7684\u6807\u51c6\u6a21\u5757\u89e3\u6790\u7b97\u6cd5\u3002\u5f53\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u5bfc\u5165\u6a21\u5757\u65f6\uff0cNode.js \u4f1a\u9996\u5148\u68c0\u67e5\u662f\u5426\u4e3a\u5185\u7f6e\u6a21\u5757\u3002\u5982\u679c\u4e0d\u662f\uff0c\u5b83\u4f1a\u7ee7\u7eed\u5728 node_modules \u76ee\u5f55\u548c\u66f4\u9ad8\u7ea7\u76ee\u5f55\u4e2d\u641c\u7d22\u3002\u8fd9\u610f\u5473\u7740\u5982\u679c\u6709\u4e00\u4e2a\u540d\u4e3a path \u7684\u81ea\u5b9a\u4e49\u6a21\u5757\u6216\u7b2c\u4e09\u65b9\u6a21\u5757\uff0c\u5b83\u5c06\u88ab\u4f18\u5148\u52a0\u8f7d\u3002<\/p>\n\n\n\n<p>\u76f8\u6bd4\u4e4b\u4e0b\uff0crequire(&#8216;node:path&#8217;) \u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u660e\u786e\u7684\u65b9\u5f0f\u6765\u5bfc\u5165\u5185\u7f6e\u6a21\u5757\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0cNode.js \u4f1a\u76f4\u63a5\u52a0\u8f7d\u5176\u5185\u7f6e\u7684 path \u6a21\u5757\uff0c\u800c\u5ffd\u7565\u4efb\u4f55\u540c\u540d\u7684\u81ea\u5b9a\u4e49\u6216\u7b2c\u4e09\u65b9\u6a21\u5757\u3002\u8fd9\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u4ee5\u907f\u514d\u6a21\u5757\u547d\u540d\u51b2\u7a81\u7684\u95ee\u9898\u3002<\/p>\n\n\n\n<p><strong>2.<\/strong><strong>\u6a21\u5757\u7f13\u5b58\u5904\u7406<\/strong><\/p>\n\n\n\n<p>\u5728 Node.js \u4e2d\uff0c\u6a21\u5757\u4e00\u65e6\u88ab\u52a0\u8f7d\uff0c\u5c31\u4f1a\u88ab\u7f13\u5b58\u3002\u8fd9\u4e24\u79cd\u5bfc\u5165\u65b9\u5f0f\u5bf9\u6a21\u5757\u7f13\u5b58\u7684\u5904\u7406\u53ef\u80fd\u6709\u6240\u4e0d\u540c\uff0c\u5f71\u54cd\u6a21\u5757\u91cd\u8f7d\u7684\u884c\u4e3a\u3002<\/p>\n\n\n\n<p><strong>3.<\/strong><strong>\u7248\u672c\u517c\u5bb9\u6027<\/strong><\/p>\n\n\n\n<p>require(&#8216;node:path&#8217;) \u7684\u4f7f\u7528\u53ef\u80fd\u4e0e Node.js \u7684\u67d0\u4e9b\u7248\u672c\u4e0d\u5b8c\u5168\u517c\u5bb9\uff0c\u7279\u522b\u662f\u5728\u65e7\u7248\u672c\u4e2d\u3002\u800c require(&#8216;path&#8217;) \u5728\u6240\u6709\u652f\u6301\u7684 Node.js \u7248\u672c\u4e2d\u90fd\u80fd\u7a33\u5b9a\u5de5\u4f5c\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"484\" height=\"300\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/11\/\u914d\u56fe-49.png?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\" class=\"wp-image-61467\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\"><strong>\u5e38\u89c1\u95ee\u7b54<\/strong><\/h2>\n\n\n\n<p><strong>1. \u5728Node.js\u4e2d\uff0crequire(node path)\u548crequire(path)\u6709\u4f55\u533a\u522b\uff1f<\/strong><\/p>\n\n\n\n<p><code>require(node path)<\/code>\u548c<code>require(path)<\/code>\u5b9e\u9645\u4e0a\u6ca1\u6709\u672c\u8d28\u7684\u533a\u522b\u3002\u5728Node.js\u4e2d\uff0c<code>node<\/code>\u5173\u952e\u5b57\u5e76\u975e\u5fc5\u9700\uff0c\u53ea\u662f\u4e3a\u4e86\u660e\u786e\u5f15\u5165\u7684\u662fNode.js\u6838\u5fc3\u6a21\u5757\u3002\u4f7f\u7528<code>require(path)<\/code>\u548c<code>require(node path)<\/code>\u4e24\u8005\u90fd\u53ef\u4ee5\u6210\u529f\u5f15\u5165\u6307\u5b9a\u8def\u5f84\u4e0b\u7684\u6a21\u5757\u6216\u6587\u4ef6\u3002<\/p>\n\n\n\n<p><strong>2. \u4e3a\u4ec0\u4e48\u6709\u4eba\u4f7f\u7528\u5e26\u6709<code>node<\/code>\u5173\u952e\u5b57\u7684require\u8bed\u6cd5\uff1f<\/strong><\/p>\n\n\n\n<p>\u4f7f\u7528<code>require(node path)<\/code>\u7684\u8bed\u6cd5\u662f\u4e00\u79cd\u660e\u786e\u8868\u660e\u5f15\u5165\u7684\u662fNode.js\u6838\u5fc3\u6a21\u5757\u7684\u505a\u6cd5\uff0c\u8fd9\u6837\u5728\u4ee3\u7801\u9605\u8bfb\u65f6\u66f4\u5bb9\u6613\u7406\u89e3\u3002\u7136\u800c\uff0c\u7531\u4e8e<code>node<\/code>\u5173\u952e\u5b57\u4e0d\u662f\u5fc5\u9700\u7684\uff0c\u5f88\u591a\u5f00\u53d1\u8005\u66f4\u503e\u5411\u4e8e\u7b80\u5316\u7684<code>require(path)<\/code>\u8bed\u6cd5\u3002<\/p>\n\n\n\n<p><strong>3. \u8fd9\u4e24\u8005\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\u6709\u6027\u80fd\u5dee\u5f02\u5417\uff1f<\/strong><\/p>\n\n\n\n<p>\u5728\u5b9e\u9645\u6267\u884c\u4e2d\uff0c<code>require(node path)<\/code>\u548c<code>require(path)<\/code>\u6ca1\u6709\u6027\u80fd\u4e0a\u7684\u672c\u8d28\u533a\u522b\u3002Node.js\u4f1a\u6839\u636e<code>path<\/code>\u6765\u786e\u5b9a\u662f\u6838\u5fc3\u6a21\u5757\u8fd8\u662f\u6587\u4ef6\u6a21\u5757\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u52a0\u8f7d\u3002\u56e0\u6b64\uff0c\u6027\u80fd\u4e0a\u7684\u5dee\u5f02\u4e3b\u8981\u53d6\u51b3\u4e8e\u52a0\u8f7d\u7684\u6a21\u5757\u672c\u8eab\uff0c\u800c\u4e0d\u662frequire\u8bed\u6cd5\u4e2d\u7684\u4e66\u5199\u65b9\u5f0f\u3002<\/p>\n\n\n\n<p><strong>4. \u6709\u6ca1\u6709\u4e00\u79cd\u8bed\u6cd5\u66f4\u63a8\u8350\u4f7f\u7528\uff1f<\/strong><\/p>\n\n\n\n<p>Node.js\u5b98\u65b9\u6587\u6863\u548c\u793e\u533a\u66f4\u503e\u5411\u4e8e\u4f7f\u7528\u7b80\u5316\u7684<code>require(path)<\/code>\u8bed\u6cd5\uff0c\u56e0\u4e3a\u5b83\u66f4\u4e3a\u6e05\u6670\u548c\u7b80\u6d01\u3002\u4f46\u5728\u7279\u5b9a\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u9700\u8981\u5f3a\u8c03\u5f15\u5165\u7684\u662fNode.js\u6838\u5fc3\u6a21\u5757\uff0c\u4f7f\u7528<code>require(node path)<\/code>\u4e5f\u662f\u5408\u6cd5\u4e14\u53ef\u8bfb\u6027\u8f83\u597d\u7684\u9009\u62e9\u3002<\/p>\n\n\n\n<p><strong>5. \u5728\u4e0d\u540c\u7684Node.js\u7248\u672c\u4e2d\uff0c\u8fd9\u4e24\u8005\u7684\u884c\u4e3a\u662f\u5426\u4e00\u81f4\uff1f<\/strong><\/p>\n\n\n\n<p>\u5728\u4e0d\u540c\u7248\u672c\u7684Node.js\u4e2d\uff0c<code>require(node path)<\/code>\u548c<code>require(path)<\/code>\u7684\u884c\u4e3a\u662f\u4e00\u81f4\u7684\uff0c\u56e0\u4e3aNode.js\u4f1a\u6839\u636e<code>path<\/code>\u7684\u503c\u6765\u52a8\u6001\u786e\u5b9a\u52a0\u8f7d\u7684\u6a21\u5757\u3002\u56e0\u6b64\uff0c\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u8bed\u6cd5\uff0c\u5176\u5728\u4e0d\u540c\u7248\u672c\u4e2d\u5e94\u8be5\u90fd\u5177\u6709\u76f8\u540c\u7684\u6548\u679c\u3002<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"\u5728 Node.js \u7f16\u7a0b\u4e2d\uff0crequire(&#8216;path&#8217;) \u548c require(&#038;#8 [&hellip;]","protected":false},"author":5,"featured_media":61467,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/61466"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=61466"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/61466\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/61467"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=61466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=61466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=61466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}