npm install 本地安裝與全局安裝的區(qū)別

npm的包安裝分為本地安裝(local)、全局安裝(global)兩種,從敲的命令行來(lái)看,差別只是有沒有-g而已,比如

npm install grunt # 本地安裝
npm install -g grunt-cli # 全局安裝

這兩種安裝方式有什么區(qū)別呢?從npm官方文檔的說明來(lái)看,主要區(qū)別在于(后面通過具體的例子來(lái)說明):
本地安裝

  1. 將安裝包放在 ./node_modules 下(運(yùn)行npm時(shí)所在的目錄)
  2. 可以通過 require() 來(lái)引入本地安裝的包

全局安裝

  1. 將安裝包放在 /usr/local 下
  2. 可以直接在命令行里使用

本地安裝

1、將安裝包放在 ./node_modules 下(運(yùn)行npm時(shí)所在的目錄)

比如運(yùn)行下面命令

npm install grunt --save-dev

那么,就會(huì)在當(dāng)前目錄下發(fā)現(xiàn)一個(gè)node_modules目錄,進(jìn)去后能夠看到grunt這個(gè)包

casperchenMacBookPro:Usemin casperchen$ ll
total 200
drwxr-xr-x  16 casperchen  staff   544B 12 14 23:17 node_modules

進(jìn)入node_modules

casperchenMacBookPro:node_modules casperchen$ ll
total 0
drwxr-xr-x  16 casperchen  staff   544B 12  5 00:49 grunt

2、可以通過 require() 來(lái)引入本地安裝的包

直接來(lái)個(gè)例子,我們?cè)陧?xiàng)目根目錄下創(chuàng)建test.js,里面的內(nèi)容很簡(jiǎn)單

var grunt = require('grunt');grunt.log.writeln('hello grunt');

然后在控制臺(tái)運(yùn)行test.js

node test.js

然后就會(huì)看到如下輸出

casperchenMacBookPro:testUsemin casperchen$ node test.js 
hello grunt

全局安裝

1、將安裝包放在 /usr/local 下

運(yùn)行如下命令

npm install -g grunt-cli

然后進(jìn)入/usr/local/bin目錄,就會(huì)發(fā)現(xiàn)grunt-cli已經(jīng)被放置在下面了

casperchenMacBookPro:bin casperchen$ pwd
/usr/local/bin
casperchenMacBookPro:bin casperchen$ ll grunt
lrwxr-xr-x  1 root  admin    39B  8 18 21:43 grunt -> ../lib/node_modules/grunt-cli/bin/grunt

可見,全局模塊的真實(shí)安裝路徑在/usr/local/lib/node_modules/下,/usr/local/bin下的可執(zhí)行文件只是軟鏈接而已

2、可以直接在命令行里使用

實(shí)現(xiàn)細(xì)節(jié)在上面其實(shí)就講到了,通過在`/usr/local/bin下創(chuàng)建軟鏈接的方式實(shí)現(xiàn)。這里不贅述

更直觀的例子

下面就直接看下,當(dāng)我們?cè)陧?xiàng)目目錄下運(yùn)行g(shù)runt task(task為具體的grunt任務(wù)名,自行替換)時(shí),發(fā)生了什么事情。這里要借助node-inspector。

首先,沒接觸過node-inspector的童鞋可以參考之前的文章了解下

運(yùn)行如下命令開啟調(diào)試

node-inspector &

見到如下輸出

casperchenMacBookPro:tmp casperchen$ node-inspector &
[1] 14390
casperchenMacBookPro:tmp casperchen$ Node Inspector v0.6.1
   info  - socket.io started
Visit http://127.0.0.1:8080/debug?port=5858 to start debugging.

接著,在當(dāng)前任務(wù)下運(yùn)行g(shù)runt任務(wù)

^CcasperchenMacBookPro:testUsemin casperchen$ node --debug-brk $(which grunt) dev
debugger listening on port 5858

接著,打開chrome瀏覽器,輸入網(wǎng)址http://127.0.0.1:8080/debug?port=5858,就會(huì)自動(dòng)進(jìn)入斷點(diǎn)調(diào)試狀態(tài)。從一旁顯示的tiPS可以看到,全局命令grunt其實(shí)就是/usr/local/lib/node_modules/grunt-cli/bin/grunt

image.png

按下F8接著往下跑,就會(huì)進(jìn)如Gruntfile.js,此時(shí)的grunt,是本地安裝的一個(gè)node包。全局命令跟本地的包名字一樣,挺有迷惑性的。
image.png

原文地址

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Node.js使得在服務(wù)器端使用JavaScript編寫應(yīng)用程序成為可能。它是基于V8Javascript運(yùn)行時(shí)并...
    間陽(yáng)幕賓閱讀 1,676評(píng)論 0 5
  • NPM NPM 是隨同 Node 一起安裝的包管理工具,能解決 Node 代碼部署上的很多問題,常見的使用場(chǎng)景有以...
    heyi_let閱讀 2,717評(píng)論 0 2
  • Nodejs的出現(xiàn),可以算是前端里程碑式的一個(gè)事件,它讓前端攻城獅們擺脫了瀏覽器的束縛,踏上了一個(gè)更加寬廣的舞臺(tái)。...
    彬_仔閱讀 2,820評(píng)論 0 3
  • 翠花小姐和酸菜歐巴是在一個(gè)網(wǎng)易游戲認(rèn)識(shí)的。原來(lái)翠花小姐叫宛,是因?yàn)樗岵藲W巴搞錯(cuò)了名字才改掉的。原來(lái)酸菜歐巴叫公子周...
    586be379241a閱讀 346評(píng)論 0 0
  • 今天是徹底掃除的日子 起床:10:00 就寢:12:30 天氣:陰 心情:平靜 紀(jì)念日:無(wú) 任務(wù)清單 昨日完成的任...
    素若初心閱讀 67評(píng)論 0 1

友情鏈接更多精彩內(nèi)容