npm i 私有庫的包例如gitlab:
git+ssh://git@github.com:npm/cli.git#v1.0.27
"axios": "latest", // 最新版本
// semver:^1.6會安裝上 1.6.68
"@rrcfe/sdk": "git+http://gitlab.rrc.com/fe/node-sdk.git#semver:^1.6",
git+https://isaacs@github.com/npm/cli.git
git://github.com/npm/cli.git#v1.0.27
git+http://gitlab.rrc.com/fe/notify-sdk.git#master // 分支
ssh:git@gitlab.rrc.com:fe-template/jenkins-scripts.git // yarn支持ssh
版本號~和^的區(qū)別:
-
~會匹配最近的小版本依賴包,比如~1.2.3會匹配所有1.2.x版本,但是不包括1.3.0 -
^會匹配最新的大版本依賴包,比如^1.2.3會匹配所有1.x.x的包,包括1.3.0,但是不包括2.0.0 - 在項目根目錄的package.json里面和 node_modules里面的 package.json 有區(qū)別
- 項目根目錄的package.json 里面 ^1.2.3 會安裝到最高的1.2.x版本,想改更高,手動改
- node_modules的 package.json 里面 ^1.2.3 會安裝到最高的1.x.x版本
dependencies 和 devDependencies(開發(fā)依賴)的區(qū)別:
-
dependencies依賴包在項目根目錄或者node_modules里面的 package.json 里面 npm i的時候 都會安裝 -
devDependencies在node_modules里面的 package.json 里面 npm i的時候 不會安裝
npm version
semver 約定一個包的版本號必須包含3個數(shù)字,格式必須為 MAJOR.MINOR.PATCH, 意為 主版本號.小版本號(增加新功能).修訂版本號(fix bug).
可以簡單地將版本號中相應(yīng)的數(shù)字加1.
npm version major|minor|patch
查看npm安裝目錄:
npm root -g
查看npm的prefix和cache路徑配置信息:
npm config get prefix
npm config get cache
修改全局和緩存路徑
- 先在設(shè)置路徑目錄下新建兩個文件夾(eg:node_global和node_cache),eg:直接在nodejsd安裝目錄下
npm config get prefix
npm config get cache
- 設(shè)置路徑 通常不用改
npm config set prefix "D:\ProgramFile\nodejs\node_modules\node_global"
npm config set cache "D:\ProgramFile\nodejs\node_modules\node_cache"
設(shè)置npm鏡像源
npm config set registry https://registry.npm.taobao.org --global
// 設(shè)置node源碼的源
npm config set disturl https://npm.taobao.org/dist --global
查看鏡像的配置結(jié)果
npm config get registry
確保 當(dāng)前源 在npm 上
npm config set registry https://registry.npmjs.org/
快捷修改.npmrc配置文件
npm config edit
// 查看
npm config list
npm 用戶名
npm whoami
打開包的官網(wǎng)
npm docs express
npm home vue
cnpm同步模塊
// 直接通過 sync 命令馬上同步一個模塊, 只有 cnpm 命令行才有此功能:
cnpm sync connect
// 當(dāng)然, 你可以直接通過 web 方式來同步: /sync/connect
open https://npm.taobao.org/sync/connect
.npmrc 文件配置只針對當(dāng)前項目
package-lock = false // 如果用yarn 加上
registry=https://registry.npm.taobao.org/
sass_binary_site=https://npm.taobao.org/mirrors/node-sass/
phantomjs_cdnurl=https://npm.taobao.org/mirrors/phantomjs
electron_mirror=https://npm.taobao.org/mirrors/electron/
.npmignore 包發(fā)布時候忽略文件,沒有這個文件會讀取.gitignore
- 想設(shè)置發(fā)布文件的白名單,設(shè)置package.json中的files屬性。
files:["package.json","src"]
- 這里的優(yōu)先級是
files>.npmignore>.gitignore
*/.Ds_store
**/.DS_Store
**/**/.DS_Store
src/.DS_Store
.DS_Store
.idea
node_modules/*
.git/
reports/*
npm-debug.log*
stats.json
/coverage
/.vscode/
.tmp
.idea
build
other
- 使用npm install下載你想要起的包名字,如果報錯404,那么你的包名是可用的。
- 使用 npm install -g 將你當(dāng)前的項目安裝到全局環(huán)境,現(xiàn)在你可以在命令行使用"my-cli"命令了。
npm addusernpm publish- 卸載
npm uninstall <pkg> 或者 npm uninstall <name>@[<version>] - 更新某一個pkg版本
npm update <pkg>@<version>
文件通配符:glob模式
-
*匹配任意多個字符,除了 / -
?匹配除了 / 之外的單個字符 -
**匹配任意多個字符,包括 / -
{}匹配用逗號分割的 or 列表 -
!用在模式的開通,表示取反.npmignore