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:查看包的源文件地址