npx命令介紹

什麼是npx

第一次看到npx命令是在 babel 的文檔裏html

Note: If you do not have a package.json, create one before installing. This will ensure proper interaction with the npx command.

在本身的機器上試了下,真的有這個命令,因而去查了下 npm 官方信息,發現這個在 npmv5.2.0引入的一條命令(連接)。引入這個命令的目的是爲了提高開發者使用包內提供的命令行工具的體驗。node

爲何引入這個命令

舉個例子,咱們開發中要運行 parcel 命令來打包:parcel index.html,之前有這麼幾種方式:react

  1. 全局安裝 parcel,但有時不一樣項目使用不一樣版本,不容許使用全局包,只能考慮下面一些方法
  2. 使用 npm scripts,在 package.json 加一個 scriptgit

    "scripts": {
        "start": "parcel index.html"
    }
  3. 將 node_modules 的可執行目錄加到 PATH 中,github

    alias npmx=PATH=$(npm bin):$PATH
    npmx parcel index.html
  4. 指定可執行命令路徑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

相關文章
相關標籤/搜索