npm init
以後一路 nextnpm install --production
安裝 package.json 裏已經聲明瞭依賴(只安裝 dependencies)npm install
安裝 package.json 裏已經聲明瞭依賴(包括:devDependencies 和 dependencies)國內 npm 官方源慢,用 nrm 能夠切換成淘寶的 npm 鏡像源html
nrm can help you easy and fast switch between different npm registries, now include: npm, cnpm, taobao, nj(nodejitsu), rednpm.node
$ npm install -g nrm
git
$ nrm use cnpm //switch registry to cnpm Registry has been set to: http://r.cnpmjs.org/
nrm 的原理是修改 npm 的registr
配置,cnpm
是 fork 一個子進程實現的(聽說有問題沒有用過,感受在發佈時會省點事,nrm 還得切回去)github
# 建議不要用 cnpm 安裝 會有各類詭異的bug 能夠經過以下操做解決 npm 下載速度慢的問題 npm install --registry=https://registry.npm.taobao.org
alias cnpmi='npm install --registry=https://registry.npm.taobao.org/'
Building fresh packages
,npm 包用到的附加沒法下載?一些 npm 包須要一些附件,這些附件一般在配置文件的 script 的 preinstall、install 或 postuninstall 中去下載,但由於牆或者網絡的緣由這些附件可能會沒法下載或下載的很慢,因此得切換到國內的源才能得到極致的體驗。sql
例如 puppeteer 的進行了以下配置在安裝的時候下載 chromium:chrome
{ "name": "puppeteer", ... "scripts": { ... "install": "node install.js", ... }, ... },
puppeteer_download_host "https://npm.taobao.org/mirrors" sass_binary_site "https://npm.taobao.org/mirrors/node-sass/" phantomjs_cdnurl "http://cnpmjs.org/downloads" electron_mirror "https://npm.taobao.org/mirrors/electron/" sqlite3_binary_host_mirror "https://foxgis.oss-cn-shanghai.aliyuncs.com/" profiler_binary_host_mirror "https://npm.taobao.org/mirrors/node-inspector/" chromedriver_cdnurl "https://cdn.npm.taobao.org/dist/chromedriver"
npmnpm
npm config set puppeteer_download_host=https://storage.googleapis.com.cnpmjs.org
yarnjson
yarn config set puppeteer_download_host https://npm.taobao.org/mirrors
參見:但願添加 chromium 鏡像源 · Issue #1246 · cnpm/cnpmjs.orgapi
安裝,卸載模塊sass
npm install 模塊名
:安裝模塊(寫入 package.json 中 dependencies 下的對應信息)
npm uninstall 模塊名
:刪除模塊(刪除 package.json 中 dependencies 下的對應信息)
[--save-dev]
寫入,刪除 package.json 中 devDependencies 下的對應信息
查找是否安裝某模塊
npm 模塊名 -v
查看安裝的模塊
npm list [-g] [-depth n]
-g:查看全局安裝的模塊
-depth n:展現 n 層模塊的依賴
PS:升級 npm 到最新版本npm install npm@latest -g
npm adduser
:輸入用戶名密碼和郵箱登錄
npm whoami
:查看登錄的用戶package.json
.npmignore
(未配置則使用.gitignore
)npm publish
:發佈
其實 npm 包就是一個帶有package.json
的文件夾。。
cd %my-package% npm install . -g # 當前包安裝到全局
或者
cd %my-package% npm link # 當前包鏈接到全局(當前包狀態改變,全局會跟着改變)
cd %some-other-folder% npm install %my-package%
npm version 版本號
:肯定版本號(格式:主版本號. 次版本號. 修訂號)(能夠爲 major | minor | patch 等)
代碼狀態 | 階段 | 規則 | 例 |
---|---|---|---|
首次發佈 | 新做品 | 從 1.0.0 開始 | 1.0.0 |
向後兼容的 Bug 修復 | Patch release | 增長最右邊第一個數字 | 1.0.1 |
向後兼容的新特性 | Minor release | 增長中間數字,並將最後一位數重置爲零 | 1.1.0 |
沒法向後兼容的更改 | Major release | 增長第一個數字,並將中間和最後一個數字重置爲零 | 2.0.0 |
參見:
使用 npm 的安全檢查。
# 檢查 npm audit [--json|--parseable] # 解決 npm audit fix [--force|--package-lock-only|--dry-run|--production|--only=dev]