這個 npm 快速參考備忘單顯示了它的常用命令使用清單
| 命令 | 描述 |
|---|---|
npm init -y | 創(chuàng)建 package.json 文件 |
npm install 或 npm i | 安裝 package.json 中的所有內容 |
npm install --production | 安裝 package.json 中的所有內容 (除了 devDependecies) |
npm install lodash | 安裝一個包 |
npm install --save-dev lodash | 安裝為 devDependency |
npm install --save-exact lodash | 準確安裝 |
npm install @scope/package-name | 安裝一個作用域的公共包 |
npm install <package_name>@<tag> | 使用 dist-tags 安裝包 |
npm install -g <package_name> | 全局安裝包 |
npm uninstall <package_name> | 卸載包 |
npm uninstall -g <package_name> | 全局卸載包 |
| 命令 | 描述 |
|---|---|
npm i sax | NPM 包 |
npm i sax@latest | 指定標簽 最新 |
npm i sax@3.0.0 | 指定版本 3.0.0 |
npm i sax@">=1 <2.0" | 指定版本范圍 |
npm i @org/sax | 范圍內的 NPM 包 |
npm i user/repo | GitHub |
npm i user/repo#master | GitHub |
npm i github:user/repo | GitHub |
npm i gitlab:user/repo | GitLab |
npm i /path/to/repo | 絕對路徑 |
npm i ./archive.tgz | 壓縮包 |
npm i https://site.com/archive.tgz | 通過 HTTP 壓縮包 |
安裝依賴的可用參數
-P, --save-prod 包將出現在您的依賴項中,這是默認值(npm v8),除非存在 -D 或 -O-D, --save-dev 包會出現在你的 devDependencies 中-O, --save-optional 包將出現在您的 optionalDependencies 中--no-save 防止保存到依賴項-E, --save-exact 依賴項將使用精確的版本進行配置,而不是使用 npm 的默認 semver 范圍運算符-B, --save-bundle 依賴項也將添加到您的 bundleDependencies 列表中命令 npm i 是 npm install 的別名
| 命令 | 描述 |
|---|---|
npm list | 列出此軟件中所有依賴項的已安裝版本 |
npm list -g --depth 0 | 列出所有全局安裝包的安裝版本 |
npm view | 列出此軟件中所有依賴項的最新版本 |
npm outdated | 僅列出此軟件中已過時的依賴項 |
$ npm cache add <package-spec> # 將指定的包添加到本地緩存
$ npm cache clean [<key>] # 刪除緩存文件夾中的所有數據
$ npm cache ls [<name>@<version>]
$ npm cache verify # 驗證緩存文件夾的內容,垃圾收集任何不需要的數據,
# 并驗證緩存索引和所有緩存數據的完整性
用于添加、列出或清理 npm 緩存文件夾
| 命令 | 描述 |
|---|---|
npm version <version> | 要更改 package.json 中的版本號 |
npm update | 更新生產包 |
npm update --dev | 更新開發(fā)包 |
npm update -g | 更新全局包 |
npm update lodash | 更新 lodash 包 |
# 將某人添加為所有者
$ npm owner add USERNAME PACKAGENAME
# 列出包
$ npm ls
# 向安裝舊版本軟件包的用戶添加警告(棄用)
$ npm deprecate PACKAGE@"< 0.2.0" "critical bug fixed in v0.2.0"
# 更新所有包或選定的包
$ npm update [-g] PACKAGE
# 檢查過時的包
$ npm outdated [PACKAGE]
$ npm unpublish <package-name> -f
# 取消指定版本
$ npm unpublish <package-name>@<version>
注意:如果您取消發(fā)布整個包,則必須在 24 小時后才能發(fā)布該包的任何新版本
# 將公共包設為私有
$ npm access restricted <package-name>
# 公開私有包
$ npm access public <package-name>
# 授予私有包訪問權限
$ npm owner add <user> <your-package-name>
# 新維護者接受邀請
$ npm owner add <their-username> <package-name>
# 刪除維護者
$ npm owner rm <your-username> <package-name>
# 寫入啟用了雙因素身份驗證
$ npm owner add <their-username> <package-name> --otp=123456
$ npm publish
# 第一次需要指定公開參數
$ npm publish --access public
$ npm publish --access public --tag previous
發(fā)布公開包,到 npmjs.org
$ npm install -g nrm # 安裝 nrm 包
# 查看 registry 列表
$ nrm ls
# 將注冊表切換到 cnpm
$ nrm use cnpm
用于設置新的或現有的 npm 包
$ npm init <package-spec> # (如同 `npx <package-spec>)
$ npm init <@scope> # (如同 `npx <@scope>/create`)
別名: create, innit
| :- | -- |
|---|---|
npm init foo | npm exec create-foo |
npm init @usr/foo | npm exec @usr/create-foo |
npm init @usr | npm exec @usr/create |
npm init @usr@2.0.0 | npm exec @usr/create@2.0.0 |
npm init @usr/foo@2.0.0 | npm exec @usr/create-foo@2.0.0 |
命令允許您在與通過 npm run 運行它類似的上下文中從 npm 包
$ npm exec -- <pkg>[@<version>] [args...]
$ npm exec --package=<pkg>[@<version>] -- <cmd> [args...]
$ npm exec -c '<cmd> [args...]'
$ npm exec --package=foo -c '<cmd> [args...]'
別名: x
$ npm exec --package yo --package generator-node --call "yo node"
$ npm exec --package=foo -- bar --bar-argument
# ~ or ~
$ npx --package=foo bar --bar-argument
從本地或遠程 npm 包運行命令
npx -- <pkg>[@<version>] [args...]
npx --package=<pkg>[@<version>] -- <cmd> [args...]
npx -c '<cmd> [args...]'
npx --package=foo -c '<cmd> [args...]'
npx 二進制文件在 npm v7.0.0 中被重寫,并且當時不推薦使用獨立的 npx 包
$ npm install eslint
# 運行:
$ ./node_modules/.bin/eslint
上面命令簡化,直接運行下面??命令
$ npx eslint
命令 npx 將自動安裝并運行 eslint
$ npx foo@latest bar --package=@npmcli/foo
# npm 將解析 foo 包名,并運行以下命令:
$ foo bar --package=@npmcli/foo
由于 npm 的參數解析邏輯,運行這個命令是不同的:
$ npm exec foo@latest bar --package=@npmcli/foo
# npm 將首先解析 --package 選項
# 解析 @npmcli/foo 包
# 然后,它將在該上下文中執(zhí)行以下命令:
$ foo@latest bar
下面命令是與 npx 等效的
$ npm exec -- foo@latest bar --package=@npmcli/foo
# 等效的
$ npx foo@latest bar --package=@npmcli/foo
使用提供的參數在本地依賴項中運行 tap 版本:
$ npm exec -- tap --bail test/foo.js
$ npx tap --bail test/foo.js
通過指定 --package 選項運行名稱與包名稱匹配的命令以外的命令:
$ npm exec --package=foo -- bar --bar-argument
# ~ or ~
$ npx --package=foo bar --bar-argument
在當前項目的上下文中運行任意 shell 腳本:
$ npm x -c 'eslint && say "hooray, lint passed"'
$ npx -c 'eslint && say "hooray, lint passed"'
$ npx react-native init AwesomeProject
$ npx react-native init AwesomeTSProject --template react-native-template-typescript
使用 npx 直接創(chuàng)建一個 React Native 應用
| :- | :- |
|---|---|
/path/to/project/.npmrc | 每個項目的配置文件 |
~/.npmrc | 每個用戶的配置文件 |
$PREFIX/etc/npmrc | 全局配置文件 |
/path/to/npm/npmrc | npm 內置配置文件 |
.npmignore將下面內容存放到 .npmignore 文件中,放置在項目的根目錄中。
.git
.svn
# 忽略 .swp 后綴的文件
.*.swp
/logs/*
# “!” 意思是不要忽視
!logs/.gitkeep
.npmignore 文件就像 .gitignore 一樣工作。它不能覆蓋 package.json#files 字段。
# 臨時使用
$ npm install -g <package-name> --registry=https://registry.npmmirror.com
將配置放置在 .npmrc 全局配置文件中,或者在項目的根目錄中。
; registry=https://registry.npmjs.org/
registry=https://registry.npmmirror.com
或者配置到 package.json#publishConfig 字段上
"publishConfig":{
"registry": "https://registry.npmmirror.com"
}
替換 npm 倉庫地址為 npmmirror(淘寶) 鏡像地址
$ npm config set registry https://registry.npmmirror.com
請參閱:npmmirror 中國鏡像站
ELECTRON_MIRROR="https://npmmirror.com/mirrors/electron/"
ELECTRON_BUILDER_BINARIES_MIRROR=https://npmmirror.com/mirrors/electron-builder-binaries/
; ELECTRON_CUSTOM_DIR="{{ version }}"
//registry.npmjs.org/:_authToken=MYTOKEN
; 將適用于 @myorg 和 @another
//somewhere.com/:_authToken=MYTOKEN
; 將適用于 @myorg
//somewhere.com/myorg/:_authToken=MYTOKEN1
; 將適用于 @another
//somewhere.com/another/:_authToken=MYTOKEN2
"type": "module" 添加到您的 package.json"main": "index.js" 替換為 "exports": "./index.js"。"engines" 字段更新為 Node.js 14: "node": ">=14.16"。(不包括 "use strict";來自所有 JavaScript 文件require() / module.export 替換為 import / exportimport x from '.'; → import x from './index.js';TypeScript 類型定義(例如 index.d.ts),請將其更新為使用 ESM 導入/導出node: 導入協議"type": "module" 添加到您的 package.json"main": "index.js" 替換為 "exports": "./index.js""engines" 字段更新為 Node.js 14: "node": ">=14.16"。 (不包括 "module": "node16", "moduleResolution": "node16" 添加到您的 tsconfig.json (列子)import x from '.'; → import x from './index.js';namespace 使用并改用 exportnode: 導入協議.ts 文件,也必須在相對導入中使用 .js 擴展名感谢您访问我们的网站,您可能还对以下资源感兴趣:
国产成人精品999视频&日本一区二区亚洲人妻精品&久久久精品国产99久久精&99热这里只有成人精品国产&精品国产剧情av一区二区&成人亚洲精品久久久久app&国产精品美女高潮抽搐A片