簡(jiǎn)單介紹
NPM是隨同NodeJS一起安裝的包管理工具礼烈,能解決NodeJS代碼部署上的很多問(wèn)題,常見(jiàn)的使用場(chǎng)景有以下幾種:
- 允許用戶從NPM服務(wù)器下載別人編寫(xiě)的第三方包到本地使用静暂。
- 允許用戶從NPM服務(wù)器下載并安裝別人編寫(xiě)的命令行程序到本地使用济丘。
- 允許用戶將自己編寫(xiě)的包或命令行程序上傳到NPM服務(wù)器供別人使用。
由于新版的nodejs已經(jīng)集成了npm洽蛀,所以之前npm也一并安裝好了摹迷。同樣可以通過(guò)輸入 **"npm -v" **來(lái)測(cè)試是否成功安裝。
常用命令
npm 安裝 Node.js 模塊語(yǔ)法格式如下:
本地安裝
$ npm install <Module Name>
將安裝包放在 ./node_modules 下(運(yùn)行 npm 命令時(shí)所在的目錄)郊供,如果沒(méi)有 node_modules 目錄峡碉,會(huì)在當(dāng)前執(zhí)行 npm 命令的目錄下生成 node_modules 目錄。
可以通過(guò) require() 來(lái)引入本地安裝的包驮审。
全局安裝
$ npm install <Module Name> -g
將安裝包放置在如下位置:
Linux: /usr/local
Windows : C:\Users\Administrator\node_modules
注意:如果是在windows下鲫寄,需要設(shè)置用戶Path=C:\Users\Administrator\AppData\Roaming\npm吉执。如不設(shè)置,安裝的命令無(wú)法執(zhí)行地来,路徑找不到
查看安裝包
# 查看安裝包
$ npm ls
# 查看全局安裝包
$ npm ls -g
卸載模塊
$ npm uninstall <Module Name>
更新模塊
$ npm update <Module Name>
搜索模塊
$ npm search <Module Name>
使用package.json
package.json 位于模塊的目錄下戳玫,用于定義包的屬性。
# 在當(dāng)前模塊目錄下生產(chǎn)package.json文件
$ npm init
# 安裝當(dāng)前package.json中定義的模塊
$ npm install
注冊(cè)與提交
我們可以使用以下命令在 npm 資源庫(kù)中注冊(cè)用戶(使用郵箱注冊(cè)):
$ npm adduser
Username: mcmohd
Password:
Email: (this IS public) mcmohd@gmail.com
然后就可以發(fā)表模塊
$ npm publish
在墻內(nèi)使用
由于眾所周知的原因未斑,很多時(shí)候npm使用出問(wèn)題咕宿。這個(gè)時(shí)候可以選擇使用淘寶鏡像
鏡像使用方法(三種辦法任意一種都能解決問(wèn)題,建議使用第三種蜡秽,將配置寫(xiě)死府阀,下次用的時(shí)候配置還在):
- 通過(guò)config命令
npm config set registry https://registry.npm.taobao.org
npm info underscore (如果上面配置正確這個(gè)命令會(huì)有字符串response)
- 命令行指定
npm --registry https://registry.npm.taobao.org info underscore
- 編輯 ~/.npmrc 加入下面內(nèi)容
registry = https://registry.npm.taobao.org
有一點(diǎn)必須注意:如果可能使用publish命令,需要將設(shè)置更換回來(lái)
另外芽突,可以選擇使用cnpm
NPM本地私服
需要一個(gè)簡(jiǎn)單的NPM本地管理
- 下載依賴速度要快
- 不會(huì)因?yàn)閚pm官方鏡像掛掉而影響開(kāi)發(fā)
- 私有模塊管理
有兩個(gè)模塊可以解決這個(gè)問(wèn)題:npm_lazy 和 sinopia 试浙,這兩個(gè)模塊的實(shí)現(xiàn)徹底消除了之前完整鏡像npm官方的痛,幾乎是零配置寞蚌,他們基本的思路基本一致:在本地運(yùn)行一個(gè)服務(wù)器實(shí)例田巴,初始化一個(gè)空的“倉(cāng)庫(kù)”,我們無(wú)需關(guān)心它是什么樣的倉(cāng)庫(kù)睬澡,當(dāng)用戶向本地服務(wù)器發(fā)起請(qǐng)求時(shí)固额,先檢查本地是否有現(xiàn)成的已更新的包,有則從本地倉(cāng)庫(kù)返回煞聪,無(wú)則從官方或指定的鏡像下載請(qǐng)求所需要的包緩存到本地并返回給用戶斗躏。
npm_lazy的使用
我們選擇npm_lazy快速的架設(shè)一個(gè)本地npm cache服務(wù)
# 安裝服務(wù)
$ npm install -g npm_lazy
# 運(yùn)行程序
$ npm_lazy
如果在內(nèi)網(wǎng)中作為私服使用,則需要進(jìn)行一下配置
var path = require('path'),
homePath = path.normalize(process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME']);
module.exports = {
// Logging config
loggingOpts: {
// Print to stdout with colors
logToConsole: true,
// Write to file
logToFile: false,
// This should be a file path.
filename: homePath + '/npm_lazy.log'
},
// Cache config
// `cacheDirectory`: Directory to store cached packages.
//
// Note: Since any relative path is resolved relative to the current working
// directory when the server is started, you should use a full path.
cacheDirectory: homePath + '/.npm_lazy',
// `cacheAge`: maximum age before an index is refreshed from remoteUrl
// - negative value means no refresh (e.g. once cached, never update the package.json metadata)
// - zero means always refresh (e.g. always ask the registry for metadata)
// - positive value means refresh every n milliseconds
// (e.g. 60 * 60 * 1000 = expire metadata every 60 minutes)
//
// Note: if you want to use `npm star` and other methods which update
// npm metadata, you will need to set cacheAge to 0. npm generally wants the latest
// package metadata version so caching package metadata will interfere with it.
// Recommended setting: 0
cacheAge: 0,
// Request config
// max milliseconds to wait for each HTTP response
httpTimeout: 10000,
// maximum number of retries per HTTP resource to get
maxRetries: 5,
// whether or not HTTPS requests are checked against Node's list of CAs
// set false if you are using your own npm mirror with a self-signed SSL cert
rejectUnauthorized: true,
// Remote and local URL
// 這里的設(shè)置要和下面的地址和端口保持一致
externalUrl: 'http://192.168.1.6:8888',
// 這里最好設(shè)置為淘寶鏡像吧昔脯,要不然龜速
remoteUrl: 'https://registry.npm.taobao.org/',
// bind port and host
// 這里是服務(wù)的端口啄糙,根據(jù)需要設(shè)置
port: 8888,
// 這里是內(nèi)網(wǎng)地址,根據(jù)情況設(shè)置
host: '192.168.1.6',
// Proxy config
// You can also configure this using the http_proxy and https_proxy environment variables
// cf. https://wiki.archlinux.org/index.php/proxy_settings
proxy: {
// http: 'http://1.2.3.4:80/',
// https: 'http://4.3.2.1:80/'
}
};
具體的配置文件位置可以通過(guò)如下命令來(lái)獲得:
npm_lazy --show-config
在win7環(huán)境下云稚,我的配置文件位置:C:\Users\用戶名\AppData\Roaming\npm\node_modules\npm_lazy\config.js
當(dāng)服務(wù)跑起來(lái)之后隧饼,需要將鏡像地址進(jìn)行設(shè)置
npm config set registry http://192.168.1.6:8888/