<div class="htmledit_views" id="content_views"> <p><br></p><p><span style="font-size:18px;">npm下載安裝的第三方模塊包官網(提供包名和使用方法):<a href="https://www.npmjs.com/" rel="nofollow">https://www.npmjs.com/</a></span><br></p><p><span style="font-size:18px;">淘寶鏡像(國內,比較快):<a href="https://npm.taobao.org/" rel="nofollow">https://npm.taobao.org/</a></span></p><p><span style="font-size:18px;"><strong>commonjs01.js:</strong></span></p><pre><code class="language-javascript hljs"><ol class="hljs-ln" style="width:1498px"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-comment">//經過npm下載安裝第三方模塊; cmd----cd 要使用第三方模塊的項目目錄----npm install 第三方模塊包名 --save install能夠簡寫成i,--save表示把安裝的模塊(依賴模塊)寫入package.json的配置文件中。</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">var</span> sd = <span class="hljs-built_in">require</span>(<span class="hljs-string">'silly-datetime'</span>); <span class="hljs-comment">//引入安裝好的第三方模塊。 npm install silly-datetime --save</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">var</span> http=<span class="hljs-built_in">require</span>(<span class="hljs-string">'http'</span>);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">var</span> app=http.createServer(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req,res</span>)</span>{</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> res.writeHead(<span class="hljs-number">200</span>,{<span class="hljs-string">"Content-Type"</span>:<span class="hljs-string">"text/html;charset=utf-8"</span>});</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">var</span> d=sd.format(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>(), <span class="hljs-string">'YYYY-MM-DD HH:mm'</span>);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> res.write(<span class="hljs-string">'你好 nodejs '</span>+d);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> res.end();</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">});</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">app.listen(<span class="hljs-number">8002</span>,<span class="hljs-string">'127.0.0.1'</span>);</div></div></li></ol></code><div class="hljs-button signin" data-title="登陸後複製" onclick="hljs.signin(event)"></div></pre><span style="font-size:18px;"><strong>commonjs02.js:</strong></span><br><pre><code class="language-javascript hljs"><ol class="hljs-ln" style="width:1182px"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-comment">//npm install md5-node //安裝第三方模塊包</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-comment">//npm install md5-node --save 或者 npm install md5-node --save-dev //注意:之後安裝模塊的時候咱們要把這個模塊寫入到package.json這個配置文件</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-comment">//npm install md5-node --save 寫入到package.json 裏面的 dependencies</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-comment">//npm install md5-node --save-dev 寫入到package.json 裏面的 devDependencies</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-comment">//npm install 表示安裝package.json 裏面的依賴(先進入項目目錄)</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-comment">//有時候的話 npm install 模塊 下載不下來 或者很慢</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-comment">//安裝淘寶鏡像 cmd----npm install -g cnpm --registry=https://registry.npm.taobao.org</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-comment">//若是安裝完成淘寶鏡像就能夠用 cnpm命令安裝</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-comment">//npm install 模塊名 ==> cnpm install 模塊名 (先進入項目目錄)</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">var</span> md5=<span class="hljs-built_in">require</span>(<span class="hljs-string">'md5-node'</span>);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-built_in">console</span>.log(md5(<span class="hljs-string">'123456'</span>));</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="16"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">var</span> sd = <span class="hljs-built_in">require</span>(<span class="hljs-string">'silly-datetime'</span>);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="17"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">var</span> d=sd.format(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>(), <span class="hljs-string">'YYYY-MM-DD'</span>);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="18"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-built_in">console</span>.log(d);</div></div></li></ol></code><div class="hljs-button signin" data-title="登陸後複製" onclick="hljs.signin(event)"></div></pre><p><span style="font-size:18px;"><strong><br></strong></span></p><p><span style="font-size:18px;"><strong>npm經常使用命令:</strong></span></p><div style="text-align:center;"><img src="https://img-blog.csdn.net/2018050817305865" alt=""></div><p></p><p><span style="font-size:18px;"><strong>3M安裝法:</strong></span></p><div style="text-align:center;"><img src="https://img-blog.csdn.net/20180508173552139" alt=""></div><pre><code class="language-sql">nrm ls 參看全部數據源(淘寶鏡像) nrm test 測試全部數據源的網絡鏈接情況 nrm <span class="hljs-keyword">use</span> cnpm 切換數據源(鏡像)javascript
若是要npm提交本身的包的時候要切換回原來的數據源。 也能夠用cnpm工具。 npm <span class="hljs-keyword">install</span> -g cnpm <span class="hljs-comment">--registry=https://registry.npm.taobao.org</span> </code></pre><br><p><br></p> 原文地址:http://www.javashuo.com/article/p-eqrejqto-gt.html </div>html