国产成人精品999视频&日本一区二区亚洲人妻精品&久久久精品国产99久久精&99热这里只有成人精品国产&精品国产剧情av一区二区&成人亚洲精品久久久久app&国产精品美女高潮抽搐A片

npm 備忘清單

這個 npm 快速參考備忘單顯示了它的常用命令使用清單

常用命令

包管理

命令描述
npm init -y創(chuàng)建 package.json 文件
npm installnpm 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 saxNPM
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/repoGitHub
npm i user/repo#masterGitHub
npm i github:user/repoGitHub
npm i gitlab:user/repoGitLab
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 inpm install 的別名

清單

命令描述
npm list列出此軟件中所有依賴項的已安裝版本
npm list -g --depth 0列出所有全局安裝包的安裝版本
npm view列出此軟件中所有依賴項的最新版本
npm outdated僅列出此軟件中已過時的依賴項

緩存 cache

$ 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]

取消發(fā)布包

$ 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 用戶帳戶

# 新維護者接受邀請
$ npm owner add <their-username> <package-name>
# 刪除維護者
$ npm owner rm <your-username> <package-name>
# 寫入啟用了雙因素身份驗證
$ npm owner add <their-username> <package-name> --otp=123456

發(fā)布包 npmjs.org

$ npm publish
# 第一次需要指定公開參數
$ npm publish --access public
$ npm publish --access public --tag previous

發(fā)布公開包,到 npmjs.org

使用 nrm 切換 registry

$ npm install -g nrm # 安裝 nrm 包
# 查看 registry 列表
$ nrm ls             
# 將注冊表切換到 cnpm
$ nrm use cnpm       

init

用于設置新的或現有的 npm

$ npm init <package-spec> # (如同 `npx <package-spec>)
$ npm init <@scope>       # (如同 `npx <@scope>/create`)

別名: create, innit

:---
npm init foonpm exec create-foo
npm init @usr/foonpm exec @usr/create-foo
npm init @usrnpm exec @usr/create
npm init @usr@2.0.0npm exec @usr/create@2.0.0
npm init @usr/foo@2.0.0npm exec @usr/create-foo@2.0.0

exec

命令允許您在與通過 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

npx

介紹

從本地或遠程 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 VS npm exec

$ 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

npx VS npm exec 示例

使用提供的參數在本地依賴項中運行 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"'

創(chuàng)建一個 React Naive 項目

$ npx react-native init AwesomeProject
$ npx react-native init AwesomeTSProject --template react-native-template-typescript

使用 npx 直接創(chuàng)建一個 React Native 應用

創(chuàng)建一個 React 應用

$ npx create-react-app my-app
$ npx create-react-app my-app --template typescript

使用 npx 跳過安裝 CRA,直接創(chuàng)建一個 React 應用

配置

.npmrc

:-:-
/path/to/project/.npmrc每個項目的配置文件
~/.npmrc每個用戶的配置文件
$PREFIX/etc/npmrc全局配置文件
/path/to/npm/npmrcnpm 內置配置文件

配置內容

# last modified: 01 Jan 2016
; Set a new registry for a scoped package
@myscope:registry=https://registry.npmmirror.com

注釋使用 #, ; 放置到一行的開頭, .npmrc 文件由指定此注釋語法的 npm/ini 解析

.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 中國鏡像站

electronjs 鏡像和緩存

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

純 ESM 包

CommonJS 項目移動到 ESM

  • "type": "module" 添加到您的 package.json
  • package.json 中的 "main": "index.js" 替換為 "exports": "./index.js"。
  • package.json 中的 "engines" 字段更新為 Node.js 14: "node": ">=14.16"。(不包括 Node.js 12,因為它不再受支持)
  • 刪除 "use strict";來自所有 JavaScript 文件
  • 將所有 require() / module.export 替換為 import / export
  • 僅使用完整的相對文件路徑進行導入:import x from '.';import x from './index.js';
  • 如果您有 TypeScript 類型定義(例如 index.d.ts),請將其更新為使用 ESM 導入/導出
  • 可選但推薦使用 node: 導入協議

TypeScript 項目輸出 ESM

  • 確保您使用的是 TypeScript 4.7 或更高版本
  • "type": "module" 添加到您的 package.json
  • package.json 中的 "main": "index.js" 替換為 "exports": "./index.js"
  • package.json 中的 "engines" 字段更新為 Node.js 14: "node": ">=14.16"。 (不包括 Node.js 12,因為它不再受支持)
  • "module": "node16", "moduleResolution": "node16" 添加到您的 tsconfig.json (列子)
  • 僅使用完整的相對文件路徑進行導入:import x from '.';import x from './index.js';
  • 刪除 namespace 使用并改用 export
  • 可選但推薦使用 node: 導入協議
  • 即使您正在導入 .ts 文件,也必須在相對導入中使用 .js 擴展名

閱讀官方 ESM 指南

另見