NPM & NPX


Posted by TempuraEngineer on 2022-05-15

NPM 與 NPX比較

NPX NPM
全名 Node Package Executor Node Package Manager
用途 不用先安裝,即可直接執行套件 安裝、刪除、升級套件
全域汙染 不發生 全域安裝的情況可能發生
使用時機 使用極少次、需使用和全域安裝不同的版本 多次使用


NPX 是甚麼

Node Package Executor,顧名思義就是用來執行node package(套件)。從NPM v5.2.0開始,只要安裝NPM,NPX也會一起被安裝, 就像安裝金山毒霸,還送軟體管家

當使用NPX執行套件時,它會先去node_modules/.bin(本地安裝路徑)、AppData/Roaming/npm(全域安裝路徑)找package binaries檔,如果有的話直接執行

When running NPM executables with NPX, if a package is installed, NPX will search for the package binaries (either locally or globally) and then run the package.

圖1

沒有的話會問要不要安裝,但並不是真的幫你裝到node_modules下,而是產出一個cache binaries,cache binaries在指令執行完後會被自動刪掉,因此不會造成全域汙染

When running NPM executables with NPX, if a package is installed, NPX will search for the package binaries (either locally or globally) and then run the package.

If the package was not previously installed, NPX will not install the package in your system; instead, it will create a temporary cache that will hold the package binaries. Once the execution is over, NPX will remove the installed cache binaries from the system.

如果要看電腦全域安裝了甚麼套件,可以去C:\Users\User\AppData\Roaming\npm\node_modules,或者使用以下指令

npm list -g


NPX的優點

  1. 避免全域汙染
  2. 不安裝極少用的套件,以節省空間
  3. 不用手動升級套件,即可享受最新版本。當然也可以指定套件版本,只是NPX預設會去抓NPM registry上套件最新版
     // 以下指令可用react v3.4.1建立專案
     npx create-react-app@3.4.1 my-app
    


📖

Comparing NPM and NPX
Are npm and npx the same?
Difference between NPM and NPX


#npm #npx







Related Posts

Decoding A/B Testing: The Magic of the Paired T-Test

Decoding A/B Testing: The Magic of the Paired T-Test

筆記、SQL 語法

筆記、SQL 語法

[ 筆記 ] 網路基礎 - HTTP, Request, Response

[ 筆記 ] 網路基礎 - HTTP, Request, Response


Comments