{"id":5128,"date":"2016-06-27T18:28:35","date_gmt":"2016-06-27T10:28:35","guid":{"rendered":"https:\/\/sdeno.com\/?p=5128"},"modified":"2018-11-21T14:17:29","modified_gmt":"2018-11-21T06:17:29","slug":"nodejs%e7%ac%94%e8%ae%b0","status":"publish","type":"post","link":"https:\/\/sdeno.com\/?p=5128","title":{"rendered":"nodejs\u7b14\u8bb0"},"content":{"rendered":"<p>node.js\u7b14\u8bb0 node\u7b14\u8bb0 npm \u7b14\u8bb0<\/p>\n<p>&nbsp;<\/p>\n<p>\u5b89\u88c5node.js<\/p>\n<pre> node -v \/\/\u51fa\u73b0\u7248\u672c\u53f7\uff0c\u5c31\u8868\u793a\u5b89\u88c5\u6210\u529f\r\n \u6267\u884cjs\u6587\u4ef6\r\n node 1.js \/\/\u6267\u884c1.js<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u6dfb\u52a0\u7cfb\u7edf\u53d8\u91cf\uff08\u4e00\u822cwin\u5b89\u88c5nodejd\u65f6\u5019\u5c31\u5df2\u7ecf\u914d\u7f6e\u597d\u4e86\uff09<\/p>\n<a href=\"https:\/\/sdeno.com\/?attachment_id=5386\" rel=\"attachment wp-att-5386\"><figure class=\"mdx-lazyload-container\" style=\"max-width:501px\"><div style=\"padding-top:103.19361277445%\"><\/div><div class=\"mdx-img-loading-sp mdui-valign\"><div><div class=\"mdui-spinner\"><\/div><\/div><\/div><img width=\"501\" height=\"517\" class=\"alignnone size-full wp-image-5386 lazyload\" title=\"win_config\" src=\"data:image\/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\" data-src=\"https:\/\/sdeno.com\/wp-content\/uploads\/2016\/06\/win_config.jpg\" alt=\"win_config\"><\/figure><\/a>\n<p>\u5728\u5f53\u524d\u7684win\u7528\u6237\u6dfb\u52a0\u4e86<\/p>\n<pre>;C:\\Program Files\\nodejs\\<\/pre>\n<p>\u4f5c\u7528\u5c31\u662f\u53ef\u4ee5\u5728\u8c03\u7528cmd\u7a97\u53e3\u4e2d\u5199node.js\u547d\u4ee4\uff0c\u5982\uff1a<\/p>\n<a href=\"https:\/\/sdeno.com\/?attachment_id=5387\" rel=\"attachment wp-att-5387\"><figure class=\"mdx-lazyload-container\" style=\"max-width:326px\"><div style=\"padding-top:62.269938650307%\"><\/div><div class=\"mdx-img-loading-sp mdui-valign\"><div><div class=\"mdui-spinner\"><\/div><\/div><\/div><img width=\"326\" height=\"203\" class=\"alignnone size-full wp-image-5387 lazyload\" title=\"win_config2\" src=\"data:image\/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\" data-src=\"https:\/\/sdeno.com\/wp-content\/uploads\/2016\/06\/win_config2.jpg\" alt=\"win_config2\"><\/figure><\/a>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u5b89\u88c5\u5b8c\u6210node\u4e4b\u540e\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528node\u7684\u5185\u7f6e\u6a21\u5757\uff0c\u5176\u4e2d\u5e38\u7528\u7684\u6709\uff1a<\/p>\n<pre>buffer\r\n fs\r\n http\r\n path\r\n querystring\r\n url\r\n \u8c03\u7528\uff1a\r\n var http=require('http')\r\n (\u6ce8\u610f\uff1a\u6ca1\u8def\u5f84\u548c.js)<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">node.js\u4e2d\u7684\u5168\u5c40\u51fd\u6570<\/span><br \/>\n\u53ef\u4ee5\u4f7f\u7528console.log(global);\u67e5\u770b\u6709\u54ea\u4e9b\u5168\u5c40\u51fd\u6570<\/p>\n<p>setTimeout<br \/>\nclearTimeout<\/p>\n<p>setInterval<br \/>\nclearInterval<\/p>\n<p>require<br \/>\nrequire.main \/\/\u68c0\u67e5\u6b64\u6a21\u5757\u662f\u5426\u4e3a\u4e3b\u6a21\u5757\u3002 node 1.js\uff0c1.js\u88ab\u79f0\u4e3a\u4e3b\u6a21\u5757<br \/>\nrequire.resolve \/\/\u83b7\u53d6\u6b64\u6a21\u5757\u672c\u5730\u8def\u5f84\u3002D:\\\\1.js<br \/>\nrequire.cache \/\/\u67e5\u770b\u7f13\u5b58\u4e86\u54ea\u4e9b\u6a21\u5757<\/p>\n<p>__filename \/\/ d:\\test\\2.js<br \/>\n__dirname \/\/ d:\\test<br \/>\n<span style=\"color: #ff0000;\">node.js\u4e8b\u4ef6<\/span>\uff08\u8ddfDOM\u53ef\u4ee5\u76d1\u542cclick\u4e8b\u4ef6\u7c7b\u4f3c\uff09<\/p>\n<p>request \/\/\u5f53\u7528\u6237\u5728\u8f93\u5165url\u56de\u8f66\u65f6\u5019\u5c31\u89e6\u53d1<\/p>\n<pre>\u00a0\u4f8b\u5982\uff1a\r\nvar http=requrie('http');\r\nvar server=http.createServer();\r\nserver.on('request',fn)  \/\/\u5f53\u670d\u52a1\u5668\u63a5\u53d7\u5230\u6570\u636e\u65f6\u5c31\u89e6\u53d1\u4e8b\u4ef6<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">node_modules\u76ee\u5f55\u7ba1\u7406\u6a21\u5757<\/span><\/p>\n<p>\u5229\u7528node_modules\u76ee\u5f55\uff0c\u53ef\u4ee5\u5728\u6b64\u76ee\u5f55\u91cc\u9762\u521b\u5efa\u7684\u6587\u4ef6\u5939\u5c31\u53ef\u4ee5\u4ee5\u6587\u4ef6\u5939\u540d\u79f0\u5f53\u4f5c\u6a21\u5757\u6765\u52a0\u8f7d\u3002<\/p>\n<p>\u81ea\u884c\u4e0b\u8f7d\uff1anode\u9879\u76ee<\/p>\n<p>\u53ea\u9700\u8981\u8fd0\u884cnode app.js\uff0c\u5c31\u80fd\u591f\u52a0\u8f7dfoo\u4e0b\u7684index.js\u6587\u4ef6\u3002npm\u7ba1\u7406\u5de5\u5177\u5176\u5b9e\u4e5f\u5c31\u662f\u7528\u8fd9\u79cd\u65b9\u5f0f\u6765\u6253\u5305\u6a21\u5757\u7684\u3002<br \/>\n<span style=\"color: #ff0000;\">\u6a21\u5757\u5c5e\u6027<\/span><br \/>\n\u4f8b\u5982\uff1a<\/p>\n<p>\/\/\u5728app.js\u6700\u540e\u9762\u6dfb\u52a0<\/p>\n<p>console.log(module.id)<br \/>\nconsole.log(module.filename) \/\/ C:\\app.js<br \/>\nconsole.log(module.loaded) \/\/true\u65f6\uff0c\u8868\u793a\u52a0\u8f7d\u5b8c\u6210<br \/>\nmodule.parent<br \/>\nmodule.children<br \/>\nnode\u5b89\u88c5\u5b8c\u6210\u540e\u5c31\u9ed8\u8ba4\u5df2\u7ecf\u4e5f\u5b89\u88c5\u4e86npm\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u901a\u8fc7npm\u53ef\u4ee5\u4e0b\u8f7d\u6211\u4eec\u9700\u8981\u7684\u6a21\u5757\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\u4e0b\u8f7djquery\u63d2\u4ef6\u4e00\u6837\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">\u6587\u4ef6\u64cd\u4f5c<\/span><br \/>\nnode\u81ea\u5e26fs\u6a21\u5757<br \/>\nvar fs=require(&#8216;fs&#8217;); \/\/\u52a0\u8f7d<br \/>\n\u521b\u5efa\u3001\u8ffd\u52a0\u5185\u5bb9<br \/>\nfs.writeFile(&#8216;.\/a.txt&#8217;,&#8217;\u54c8\u54c8&#8217;,fn) \/\/\u521b\u5efa\u6587\u4ef6\u548c\u5185\u5bb9<br \/>\nfs.writeFile(&#8216;.\/a.txt&#8217;,&#8217;\u8ffd\u52a0\u5185\u5bb9&#8217;,{flag:&#8217;a&#8217;},fn) \/\/\u8ffd\u52a0\u5185\u5bb9\uff0c\u4e0d\u52a0{flag:&#8217;a&#8217;}\u5219\u662f\u8986\u76d6\u5185\u5bb9<br \/>\nfs.appendFile(&#8216;.\/a.txt&#8217;,&#8217;\u8981\u8ffd\u52a0\u7684\u5185\u5bb9&#8217;,&#8217;utf8&#8242;,fn) \/\/\u8ffd\u52a0\u5185\u5bb9<\/p>\n<p>&nbsp;<\/p>\n<p>\u67e5\u770b\u5185\u5bb9<br \/>\nfs.readFile(&#8216;.\/1.txt&#8217;,&#8217;utf8&#8242;,function(){}) \/\/\u5f02\u6b65\uff0c\u53ef\u4ee5\u4f7f\u7528\u56de\u8c03\u5f62\u5f0f\u3002\u4e00\u822c\u60c5\u51b5\u90fd\u4f7f\u7528\u8fd9\u4e2a<br \/>\nfs.readFileSync(&#8216;.\/1.txt&#8217;,&#8217;utf8&#8242;) \/\/\u540c\u6b65\uff0c\u4e0d\u80fd\u4f7f\u7528\u56de\u8c03\u5f62\u5f0f\u3002\u5f88\u5c11\u7528<\/p>\n<p>&nbsp;<\/p>\n<p>\u76ee\u5f55\u521b\u5efa\u3001\u8bfb\u53d6<\/p>\n<p>fs.mkdir(&#8216;.\/mk&#8217;,fn) \/\/\u521b\u5efa\u4e00\u4e2amk\u76ee\u5f55<br \/>\nfs.readdir(&#8216;.\/&#8217;,function(err,dir){}) \/\/\u8bfb\u53d6\u76ee\u5f55<\/p>\n<p>&nbsp;<\/p>\n<p>\u67e5\u770b\u6587\u4ef6\u5c5e\u6027<br \/>\nfs.stat(&#8216;.\/a.txt&#8217;,function(err,stats){})<br \/>\nfs.lstat(&#8216;.\/a.txt&#8217;,function(err,stats){}) \/\/\u67e5\u770b<\/p>\n<p>&nbsp;<\/p>\n<p>\u68c0\u67e5\u6587\u4ef6\u3001\u76ee\u5f55\u662f\u5426\u5b58\u5728<br \/>\nfs.exists()<\/p>\n<p>&nbsp;<\/p>\n<p>\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84<br \/>\nfs.realpath<\/p>\n<p>&nbsp;<\/p>\n<p>\u79fb\u52a8\u6587\u4ef6<br \/>\nfs.rename<\/p>\n<p>&nbsp;<\/p>\n<pre>\/\/\u652f\u6301\u4e8b\u4ef6\r\nfs.createReadStream('.\/1.txt',{encoding:'utf8'})\r\n .on('open',function(){\r\n    console.log('\u8bfb\u53d6\u6587\u4ef6');\r\n })\r\n .on('data',function(data){\r\n    console.log(data)\r\n })\r\n .on('end',function(){\r\n    console.log('\u5185\u5bb9\u90fd\u8bfb\u53d6\u5b8c\u4e86');\r\n })\r\n .on('close',function(){\r\n    console.log('\u6587\u4ef6\u88ab\u5173\u95ed');\r\n })\r\n .on('error',function(){\r\n    console.log('\u8bfb\u53d6\u6587\u4ef6\u5931\u8d25');\r\n });<\/pre>\n<p>&nbsp;<\/p>\n<p>\uff08fs\u6a21\u5757\u529f\u80fd\u8fc7\u591a\uff0c\u5177\u4f53\u81ea\u884c\u67e5\u8d44\u6599\uff09<\/p>\n<p>&nbsp;<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n<span style=\"color: #ff0000;\">path\u6a21\u5757<\/span> (\u8def\u5f84\u64cd\u4f5c)<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n<span style=\"color: #ff0000;\">http\u6a21\u5757<\/span> \uff08\u63d0\u4f9bhttp\u548chttps\u670d\u52a1\u5668\uff09<\/p>\n<pre>var http=require('http');\r\n http.createServer(function(req,res){\r\n   req \/\/\u83b7\u53d6\u5ba2\u6237\u7aef\u53d1\u6765\u7684\u4fe1\u606f\r\n   res \/\/\u83b7\u53d6\u670d\u52a1\u5668\u7aef\u53d1\u6765\u7684\u4fe1\u606f\r\n }).listen('1337','127.0.0.1');<\/pre>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n<span style=\"color: #ff0000;\">url \u53c2\u6570\u64cd\u4f5c<\/span><br \/>\nnode\u63d0\u4f9burl\u6a21\u5757\u548cquerystring\u6a21\u5757<\/p>\n<pre>\/\/querystring\u6a21\u5757\r\nquerystring.parse('user=123&amp;name=\u9648','&amp;');\r\n{ user: '123', name: '\u9648' }<\/pre>\n<pre>\/\/url\u6a21\u5757\r\nurl.parse('http:\/\/127.0.0.1:1338\/1.php?user=hello&amp;name=chenchen#hash')\r\n{\r\n protocol: 'http:',\r\n slashes: true,\r\n auth: null,\r\n host: '127.0.0.1:1338',\r\n port: '1338',\r\n hostname: '127.0.0.1',\r\n hash: '#hash',\r\n search: '?user=hello&amp;name=chenchen',\r\n query: 'user=hello&amp;name=chenchen',\r\n pathname: '\/1.php',\r\n path: '\/1.php?user=hello&amp;name=chenchen',\r\n href: 'http:\/\/127.0.0.1:1338\/1.php?user=hello&amp;name=chenchen#hash' \r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">npm\u4f7f\u7528<\/span><\/p>\n<p><strong>\u589e<\/strong><br \/>\nnpm install \/\/\u5b89\u88c5package.json\u4e2d\u7684\u6240\u6709\u5305<br \/>\nnpm install gulp \/\/\u5c40\u90e8\u5b89\u88c5<br \/>\nnpm install -g gulp \/\/\u5168\u5c40\u5b89\u88c5<br \/>\nnpm install express@3.0 \/\/\u5b89\u88c5\u6307\u5b9a\u7248\u672c<br \/>\n\uff08\u5168\u5c40\u5b89\u88c5\u548c\u5c40\u90e8\u5b89\u88c5\u7684\u533a\u522b\u5c31\u662f\uff0c\u5b89\u88c5\u5728\u5168\u5c40\u7684\u6a21\u5757\u5305\u597d\u5904\u4e5f\u5c31\u662f\u5185\u7f6e\u7684\u547d\u4ee4\u53ef\u4ee5\u5728\u4efb\u610f\u76ee\u5f55\u4e0b\u4f7f\u7528\uff0c\u5982\u679c\u8981\u8c03\u7528\u6a21\u5757\uff0c\u4f8b\u5982<br \/>\nvar express =require(&#8216;express&#8217;),\u5fc5\u987b\u5b89\u88c5\u5728\u5c40\u90e8\u76ee\u5f55\u4e2d\u3002\u5bf9\u4e8e\u90a3\u4e9b\u8981\u547d\u4ee4\u64cd\u4f5c\u7684\u6a21\u5757\u5305\u63a8\u8350\u5b89\u88c5\u5728\u5168\u5c40\u4e2d\uff09<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u5220<\/strong><br \/>\nnpm uninstall gulp \/\/\u5378\u8f7d\u5c40\u90e8\u6a21\u5757<br \/>\nnpm uninstall -g gulp \/\/\u5378\u8f7d\u5168\u5c40\u6a21\u5757<br \/>\nnpm cache clean \/\/\u6e05\u695a\u7f13\u5b58\u5305<br \/>\nnpm uninstall express@4.10.2 \/\/\u5378\u8f7d\u6307\u5b9a\u7248\u672c:<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u6539<\/strong><br \/>\nnpm config set prefix &#8220;d:\\node&#8221; \/\/\u4fee\u6539\u5168\u5c40\u5b89\u88c5\u8def\u5f84\uff0c\u4ee5\u540e\u5168\u5c40\u5b89\u88c5\u8def\u5f84\u5c31\u662fd:\\node\\node_modules \u63a8\u8350<br \/>\n\u770b\uff1a<a href=\"https:\/\/sdeno.com\/?p=4740\" target=\"_blank\" rel=\"noopener\">https:\/\/sdeno.com\/?p=4740<\/a><br \/>\nnpm update gulp \/\/\u5347\u7ea7\u5c40\u90e8\u8def\u5f84\u7684\u5305<br \/>\nnpm update -g gulp \/\/\u5347\u7ea7\u5c40\u90e8\u8def\u5f84\u7684\u5305<br \/>\nnpm update \/\/\u5347\u7ea7\u5c40\u90e8\u8def\u5f84\u7684\u6240\u6709\u5305<br \/>\nnpm update -g \/\/\u5347\u7ea7\u5168\u5c40\u8def\u5f84\u6240\u6709\u5305<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u67e5<\/strong><br \/>\nnpm root -g \/\/\u67e5\u770b\u5168\u5c40\u6a21\u5757\u5b89\u88c5\u5305\u7684\u8def\u5f84\uff0c\u5982d:\\npmConfig\\npm\\node_modules<br \/>\nnpm config get prefix \/\/\u67e5\u770bnpm\u5f53\u524d\u5b89\u88c5\u8def\u5f84\uff0c\u5982d:\\npmConfig\\npm<br \/>\nnpm list \/\/\u67e5\u770b\u5c40\u90e8\u8def\u5f84\u5b89\u88c5\u4e86\u54ea\u4e9b\u6a21\u5757\uff0c\u5982\u679c\u6ca1\u6709package.json\u5c31\u4f1a\u62a5\u9519<br \/>\nnpm list -g \/\/\u67e5\u770b\u5168\u5c40\u5b89\u88c5\u4e86\u54ea\u4e9b\u6a21\u5757\u7684\u8be6\u7ec6\u4fe1\u606f<br \/>\nnpm list &#8211;global \/\/\u540c\u4e0a<br \/>\nnpm list -g &#8211;depth=0 \/\/\u53ea\u663e\u793a\u5168\u5c40\u6a21\u5757\u5df2\u5b89\u88c5\u7684\u5305\u548c\u7248\u672c\u53f7\uff0c\u907f\u514d\u5197\u957f<br \/>\nnpm search express \/\/\u641c\u7d22\u5305\u662f\u5426\u5b58\u5728<br \/>\nnpm config ls -l \/\/\u67e5\u770b\u5f53\u524dnpm\u7684\u8bbe\u7f6e<br \/>\nnpm config get cache \/\/\u67e5\u770bnpm\u7f13\u5b58\u76ee\u5f55<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">\u521b\u5efapackage.json \u00a0\u7ba1\u7406\u4f9d\u8d56\u6a21\u5757<\/span><\/p>\n<pre>npm init\r\n\r\nnpm init --yes  \/\/\u5feb\u901f\u521b\u5efa<\/pre>\n<p>\u5b89\u88c5\u63d0\u793a\u4e00\u76f4\u56de\u8f66\uff1a<\/p>\n<a href=\"https:\/\/sdeno.com\/wp-content\/uploads\/2016\/06\/pack123.jpg\"><figure class=\"mdx-lazyload-container\" style=\"max-width:529px\"><div style=\"padding-top:88.846880907372%\"><\/div><div class=\"mdx-img-loading-sp mdui-valign\"><div><div class=\"mdui-spinner\"><\/div><\/div><\/div><img width=\"529\" height=\"470\" class=\"alignnone size-full wp-image-5152 lazyload\" title=\"pack123\" src=\"data:image\/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\" data-src=\"https:\/\/sdeno.com\/wp-content\/uploads\/2016\/06\/pack123.jpg\" alt=\"pack123\" data-srcset=\"https:\/\/sdeno.com\/wp-content\/uploads\/2016\/06\/pack123.jpg 529w, https:\/\/sdeno.com\/wp-content\/uploads\/2016\/06\/pack123-300x266.jpg 300w\" sizes=\"auto, (max-width: 529px) 100vw, 529px\"><\/figure><\/a>\n<p>yes\u4ee5\u540e\uff0c\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u5c31\u80fd\u770b\u89c1package.json<\/p>\n<p>\u4ee5\u540e\u5b89\u88c5\u6a21\u5757\u65f6\u4f7f\u7528&#8211;save-dev\u540e\u7f00\uff0c\u5378\u8f7d\u65f6\u4e5f\u4e00\u6837devDependencies\u5b57\u6bb5\u4f1a\u66f4\u65b0\u8fd9\u6837\u80fd\u4fdd\u6301\u5b9e\u65bd\u66f4\u65b0package.json\u6587\u4ef6\uff0c\u5982<\/p>\n<pre>\/\/\u5b89\u88c5\r\nnpm install mysql --save-dev<\/pre>\n<pre>\/\/\u5378\u8f7d\r\nnpm uninstall mysql --save-dev<\/pre>\n<pre>\/\/\u6279\u91cf\u5b89\u88c5,\u8bb0\u5f55\u5728package.json\u4e2d\u7684\u6a21\u5757\u5c06\u4f1a\u81ea\u52a8\u6279\u91cf\u5b89\u88c5\r\nnpm install<\/pre>\n<p>\u5982\u679c\u4f7f\u7528&#8211;save\u540e\u7f00\u662fdependencies\u5b57\u6bb5\u66f4\u65b0\u3002<\/p>\n<pre>-S\uff1a--save\u7684\u7b80\u5199,\u5b89\u88c5\u7684\u6a21\u5757\u4e00\u822c\u662f\u6b63\u5f0f\u73af\u5883\u9700\u8981\u7684\uff0c\u4f1a\u8bb0\u5f55\u5728dependencies\u5b57\u6bb5\u4e2d\r\n-D\uff1a--save-dev\u7684\u7b80\u5199\uff0c\u4e00\u822c\u662f\u7528\u4e8e\u6d4b\u8bd5\u65f6\u6240\u9700\u8981\u7684\u6a21\u5757\uff0c\u6b63\u5f0f\u4e0a\u7ebf\u662f\u4e0d\u9700\u8981\u7684\u8bb0\u5f55\u5728devDependencies\u5b57\u6bb5\u4e2d<\/pre>\n<p>devDependencies\uff1a\u4e3b\u8981\u5b89\u88c5\u6b64\u5b57\u6bb5\u7684\u6a21\u5757\u662f\u7528\u6237\u7528\u4e8e\u9879\u76ee\u4f9d\u8d56\u7684\u6a21\u5757\uff0c\u5bf9\u6b63\u5f0f\u4e0a\u7ebf\u7684\u9879\u76ee\u6765\u8bf4\u662f\u591a\u4f59\u7684\u4ec5\u4ec5\u662f\u7528\u6765\u6d4b\u8bd5\u800c\u5df2\u3002<\/p>\n<p>dependencies\uff1a\u8be5\u5b57\u6bb5\u91cc\u9762\u7684\u6a21\u5757\u662f\u6b63\u5f0f\u4e0a\u7ebf\u7684\u9879\u76ee\u6b63\u5219\u6240\u4f9d\u8d56\u7684\uff0c\u7f3a\u5c11\u4e00\u4e2a\u90fd\u4e0d\u884c\u3002<\/p>\n<p>&nbsp;<\/p>\n<pre>\"scripts\": {\r\n  \"preinstall\": \"echo here it comes!\",\r\n  \"postinstall\": \"echo there it goes!\",\r\n  \"start\": \"node index.js\",\r\n  \"test\": \"tap test\/*.js\"\r\n}\r\n\r\n\u5c06\u8fc7\u957f\u7684\u547d\u4ee4\u7b80\u7ea6\u5316\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u6267\u884cnpm run preinstall\u76f8\u5f53\u4e8e\u6267\u884c\u4e86echo here it comes!\u547d\u4ee4<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">\u5728node_modules\u76ee\u5f55\u4e2d\u521b\u5efa\u5c5e\u4e8e\u81ea\u5df1\u7684\u6a21\u5757<\/span><\/p>\n<pre>\/\/\u5728node_modules\u76ee\u5f55\u91cc\u9762\u521b\u5efa\u540d\u4e3aceshi\u7684\u76ee\u5f55\uff0c\u5e76\u5728ceshi\u76ee\u5f55\u91cc\u9762\u521b\u5efaindex.js,\u5185\u5bb9\u4e3a\uff1a\r\n\r\nfunction xx() {\r\n  console.log('\u6d4b\u8bd5');\r\n}\r\n\r\nexports.xx=xx;\r\n\r\n\uff08\u5728node_modules\u7684\u6a21\u5757\u4e2d\u5165\u53e3\u540d\u79f0\u9ed8\u8ba4\u662findex.js\u5982\u679c\u60f3\u81ea\u5b9a\u4e49\u5c31\u5728\u6a21\u5757\u6839\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2apackage.json,\u5e76\u5728\u4fee\u6539\"main\":\"xxx.js\"\u5373\u53ef\uff09<\/pre>\n<p>&nbsp;<\/p>\n<p>\u8c03\u7528\uff1a<\/p>\n<p>\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a1.js\u7684\u6587\u4ef6<\/p>\n<pre>var get_xx=require('ceshi');\r\n\r\nget_xx.xx();<\/pre>\n<p>\u8fd0\u884cnode 1.js\u5c31\u53ef\u4ee5\u8c03\u7528node_modules\u91cc\u9762\u7684ceshi\u6a21\u5757\u4e86<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><audio style=\"display: none;\" controls=\"controls\"><\/audio><\/p>\n<p><audio style=\"display: none;\" controls=\"controls\"><\/audio><\/p>\n<p><audio style=\"display: none;\" controls=\"controls\"><\/audio><\/p>\n","protected":false},"excerpt":{"rendered":"<p>node.js\u7b14\u8bb0 node\u7b14\u8bb0 npm \u7b14\u8bb0 &nbsp; \u5b89\u88c5node.js node -v \/\/\u51fa\u73b0\u7248\u672c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-5128","post","type-post","status-publish","format-standard","hentry","category-nodejs"],"_links":{"self":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/5128","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5128"}],"version-history":[{"count":0,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/5128\/revisions"}],"wp:attachment":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}