npx
第一次看到npx
命令是在 babel 的文檔裏html
Note: If you do not have apackage.json
, create one before installing. This will ensure proper interaction with thenpx
command.
在本身的機器上試了下,真的有這個命令,因而去查了下 npm 官方信息,發現這個在 npmv5.2.0
引入的一條命令(連接)。引入這個命令的目的是爲了提高開發者使用包內提供的命令行工具的體驗。node
舉個例子,咱們開發中要運行 parcel 命令來打包:parcel index.html
,之前有這麼幾種方式:react
使用 npm scripts,在 package.json 加一個 scriptgit
"scripts": { "start": "parcel index.html" }
將 node_modules 的可執行目錄加到 PATH 中,github
alias npmx=PATH=$(npm bin):$PATH npmx parcel index.html
指定可執行命令路徑npm
./node_modules/.bin/parcel index.html
如今咱們有了 npx
命令,就不在須要考慮以上方法了(其實npx
是對方法 3 的封裝)。當咱們執行 npx parcel index.html
時,會自動去./node_modules/.bin
目錄下搜索。json
npx
還容許咱們單次執行命令而不須要安裝,例如bash
npx create-react-app my-cool-new-app
這條命令會臨時安裝 create-react-app 包,命令完成後 create-react-app 會刪掉,不會出如今 global 中。下次再執行,仍是會從新臨時安裝。babel