1、NPM 簡介
- NPM(node package manager),通常稱為 node 包管理器,是目前世界上最大的開源庫生態系統。使用 NPM 可以對 node 包進行安裝、卸載、更新、查看、搜索、發布等操作。安裝完 Node.js®,NPM 就可以直接用了。
2、NPM 環境配置
2.1 安裝 Node.js
-
從下面的地址中下載安裝 Node.js。
- Node.js 官網下載地址。
- Node.js 其它下載地址
- Node v9.7.1 for Mac,密碼:svmp。
- Node v10.1.0 for Mac,密碼:y76y。
- Node v10.2.0 for Mac,密碼:buzn。
-
或者直接使用 Homebrew 按照下面的命令安裝 Node.js。
# 安裝 Node.js $ brew install node
2.2 安裝 NPM
- 安裝完 Node.js,NPM 就可以直接用了。
2.3 NPM 常用命令
-
1)NPM 常用命令
# 查看 NPM 版本 $ npm -v # 更新 NPM 版本 $ sudo npm i -g npm # 在官方包倉庫中搜索包 $ npm search [包名] $ npm search express # 查看官方包倉庫中包所用 package.json 文件中的信息 $ npm view [包名] $ npm view express # 安裝 package.json 文件中列出的包 $ npm install # 安裝包 # 安裝在本地項目中 $ npm install [包名] $ npm install express # 安裝在全局中 $ npm install -g [包名] $ npm install -g express # 安裝並保存包到 package.json 文件中 # 安裝在本地項目中 $ npm install [包名] --save $ npm install express --save # 安裝在全局中 $ npm install -g [包名] --save $ npm install -g express --save # 查看安裝的包 # 本地 $ npm list # 全局 $ npm list -g # 查看過期的包 # 本地 $ npm outdated # 全局 $ npm outdated -g # 更新全部包 # 本地 $ npm update # 全局 $ npm update -g # 更新指定的包 # 本地 $ npm update [包名] $ npm update express # 全局 $ npm update -g [包名] $ npm update -g express # 卸載包 # 本地 $ npm uninstall [包名] $ npm uninstall express # 全局 $ npm uninstall -g [包名] $ npm uninstall -g express # 查看全局包的安裝路徑 $ npm root -g # 修改全局包的安裝路徑,修改后為 [安裝路徑]\node_modules\ $ npm config set prefix "[安裝路徑]"Usage: npm <command> where <command> is one of: access, adduser, bin, bugs, c, cache, completion, config, ddp, dedupe, deprecate, dist-tag, docs, doctor, edit, explore, get, help, help-search, i, init, install, install-test, it, link, list, ln, login, logout, ls, outdated, owner, pack, ping, prefix, profile, prune, publish, rb, rebuild, repo, restart, root, run, run-script, s, se, search, set, shrinkwrap, star, stars, start, stop, t, team, test, token, tst, un, uninstall, unpublish, unstar, up, update, v, version, view, whoami npm <command> -h quick help on <command> npm -l display full usage info npm help <term> search for help on <term> npm help npm involved overview Specify configs in the ini-formatted file: /Users/qianchia/.npmrc or on the command line via: npm <command> --key value Config info can be viewed via: npm help config
2.4 注意事項
-
一般 Node modules 通常被安裝在每個項目的本地文件夾
node_modules中,但下面幾個包推薦安裝在全局:CoffeeScript、Less、Grunt或Gulp
# 安裝 coffee-script $ npm install -g coffee-script # 安裝 less $ npm install -g less # 安裝 grunt-cli $ npm install -g grunt-cli # 安裝 gulp $ npm install -g gulp
3、CNPM 配置
3.1 CNPM 簡介
-
因為 npm 安裝插件是從國外服務器下載,受網絡影響大,可能出現異常,如果 npm 的服務器在中國就好了,所以淘寶團隊干了這事。來自官網:“這是一個完整 npmjs.org 鏡像,你可以用此代替官方版本(只讀),同步頻率目前為 10 分鍾一次以保證盡量與官方服務同步。”
-
cnpm 跟 npm 用法完全一致,只是在執行命令時將 npm 改為 cnpm。
3.2 CNPM 環境配置
-
在終端中輸入以下命令安裝 CNPM。
# 安裝 CNPM $ npm install -g cnpm --registry=https://registry.npm.taobao.org -
安裝完后最好查看其版本號
cnpm -v或重啟終端,安裝完直接使用有可能會出現錯誤。
