1. Node.js安裝
1.1 Node.js下載
Node.js官網:https://nodejs.org
當前下載版本(含npm):Latest LTS Version: v6.10.3 (includes npm 3.10.10)
1.2 Node.js及npm檢視版本
安裝Node.js之後,檢視Node.js及npm版本。
node -v
npm -v
2. npm常用命令
npm(node package manager),node包管理器,主要功能是管理node包,包括:安裝、解除安裝、更新、檢視、搜尋、釋出等。
npm官網文件:https://docs.npmjs.com/
2.1 npm配置
npm install -g cnpm --registry=https://registry.npm.taobao.org
2.2 npm包管理
◊ npm init:在專案中引導建立一個package.json檔案
npm init [-f|--force|-y|--yes]
PS F:\Projects\Libing.Portal> npm init This utility will walk you through creating a package.json file. It only covers the most common items, and tries to guess sensible defaults. See `npm help json` for definitive documentation on these fields and exactly what they do. Use `npm install <pkg> --save` afterwards to install a package and save it as a dependency in the package.json file. Press ^C at any time to quit. name: (Libing.Portal) libing.portal version: (1.0.0) description: entry point: (index.js) main.js test command: git repository: keywords: author: libing license: (ISC) MIT About to write to F:\Projects\Libing.Portal\package.json: { "name": "libing.portal", "version": "1.0.0", "description": "", "main": "main.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "libing", "license": "MIT" } Is this ok? (yes)
◊ npm install:安裝包
npm install命令引數形式:
npm install --help
npm install (with no args, in package dir) npm install [<@scope>/]<pkg> npm install [<@scope>/]<pkg>@<tag> npm install [<@scope>/]<pkg>@<version> npm install [<@scope>/]<pkg>@<version range> npm install <folder> npm install <tarball file> npm install <tarball url> npm install <git:// url> npm install <github username>/<github project> aliases: i, isntall common options: [--save|--save-dev|--save-optional] [--save-exact]
示例:
npm install angular
執行之後將建立資料夾node_modules,預設安裝包最新版本。
指定安裝包版本:
npm install angular@1.2.32
-S, --save 安裝包資訊將加入到dependencies(生產階段的依賴):
npm install angular -S
"dependencies": { "angular": "^1.6.4" },
-D, --save-dev 安裝包資訊將加入到devDependencies(開發階段的依賴):
npm install angular -D
"devDependencies": { "angular": "^1.6.4" }
安裝包的依賴都被寫入了package.json檔案後,可以使用npm install根據dependencies配置安裝全部依賴包。
npm install
◊ npm uninstall:解除安裝包
npm uninstall命令引數形式:
npm uninstall -help
npm uninstall [<@scope>/]<pkg>[@<version>]... [--save|--save-dev|--save-optional]
npm uninstall angular -S
◊ npm list:檢視全部已安裝包
npm list
◊ npm outdated檢查包是否過時
npm outdated
◊ npm update:更新包
npm update [-g] [<pkg>...]
◊ npm view:檢視包的註冊資訊
npm view [<@scope>/]<pkg>[@<version>] [<field>[.subfield]...]
npm view angular
npm view angular dependencies:檢視包的依賴關係
npm view angular repository.url:檢視包的原始檔地址