npx npm區別
If you’ve ever used Node.js, then you must have used npm for sure.
如果您曾經使用過Node.js ,那么一定要使用npm 。
npm (node package manager) is the dependency/package manager you get out of the box when you install Node.js. It provides a way for developers to install packages both globally and locally.
npm (節點程序包管理器)是您在安裝Node.js時開箱即用的依賴項/程序包管理器。 它為開發人員提供了一種在全球和本地安裝軟件包的方法。
Sometimes you might want to take a look at a specific package and try out some commands. But you cannot do that without installing the dependencies in your local node_modules
folder.
有時,您可能希望查看特定的軟件包并嘗試一些命令。 但是,如果不在本地node_modules
文件夾中安裝依賴項,就無法做到這node_modules
。
That’s where npx comes in.
那就是npx出現的地方。
In this article, we’re going to have a look at the differences between the npm and npx and learn how to get the best from both.
在本文中,我們將研究npm和npx之間的區別,并學習如何從這兩者中獲得最大的收益 。
First, let’s understand what npm actually is and what we can do with it.
首先,讓我們了解npm實際上是什么以及我們可以使用它做什么。
npm包管理器 (npm the package manager)
npm is a couple of things. First and foremost, it is an online repository for the publishing of open-source Node.js projects.
npm是幾件事情。 首先,它是一個在線資源庫,用于發布開源Node.js項目。
Second, it is a CLI tool that aids you install those packages and manage their versions and dependencies. There are hundreds of thousands of Node.js libraries and applications on npm and many more are added every day.
其次,它是一個CLI工具,可幫助您安裝這些軟件包并管理其版本和依賴性。 npm上有成千上萬個Node.js庫和應用程序,并且每天都在增加。
npm by itself doesn’t run any packages. If you want to run a package using npm, you must specify that package in your package.json
file.
npm本身不會運行任何軟件包。 如果要使用npm運行軟件包,則必須在package.json
文件中指定該軟件包。
When executables are installed via npm packages, npm creates links to them:
通過npm軟件包安裝可執行文件時,npm會創建指向它們的鏈接:
local installs have links created at the
./node_modules/.bin/
directory本地安裝具有在
./node_modules/.bin/
目錄中創建的鏈接global installs have links created from the global
bin/
directory (for example:/usr/local/bin
on Linux or at%AppData%/npm
on Windows)全局安裝具有從全局
bin/
目錄創建的鏈接(例如:Linux上為/usr/local/bin
或Windows上為%AppData%/npm
)
To execute a package with npm you either have to type the local path, like this:
要使用npm執行軟件包,您必須輸入本地路徑,如下所示:
$ ./node_modules/.bin/your-package
or you can run a locally installed package by adding it into your package.json
file in the scripts section, like this:
或者您可以通過將其添加到腳本部分的package.json
文件中來運行本地安裝的軟件包,如下所示:
{"name": "your-application","version": "1.0.0","scripts": {"your-package": "your-package"}
}
Then you can run the script using npm run
:
然后,您可以使用npm run
運行腳本:
npm run your-package
You can see that running a package with plain npm requires quite a bit of ceremony.
您會看到,使用純npm運行軟件包需要大量的儀式。
Fortunately, this is where npx comes in handy.
幸運的是,這是npx派上用場的地方。
npx包運行器 (npx the package runner)
Since npm version 5.2.0 npx is pre-bundled with npm. So it’s pretty much a standard nowadays.
從npm 5.2.0版本開始, npx與npm預先捆綁在一起。 因此,這已成為當今的標準。
npx is also a CLI tool whose purpose is to make it easy to install and manage dependencies hosted in the npm registry.
npx還是一個CLI工具,其目的是使安裝和管理npm注冊表中托管的依賴項變得容易。
It’s now very easy to run any sort of Node.js based executable that you would normally install via npm.
現在,運行通常通過npm安裝的各種基于Node.js的可執行文件非常容易。
You can run the following command to see if it is already installed for your current npm version:
您可以運行以下命令,以查看當前npm版本是否已安裝該命令:
$ which npx
If it's not, you can install it like this:
如果不是,則可以這樣安裝:
$ npm install -g npx
Once you make sure you have it installed, let’s see a few of the use cases that make npx extremely helpful.
一旦確定已安裝它,就讓我們看一些使npx非常有用的用例。
輕松運行本地安裝的軟件包 (Run a locally installed package easily)
If you wish to execute a locally installed package, all you need to do is type:
如果要執行本地安裝的軟件包,只需輸入:
$ npx your-package
npx will check whether <command>
or <package>
exists in $PATH
, or in the local project binaries, and if so it will execute it.
npx將檢查$PATH
或本地項目二進制文件中是否存在<command>
或<package>
,如果存在,它將執行該命令。
執行以前未安裝的軟件包 (Execute packages that are not previously installed)
Another major advantage is the ability to execute a package that wasn’t previously installed.
另一個主要優點是能夠執行以前未安裝的軟件包。
Sometimes you just want to use some CLI tools but you don’t want to install them globally just to test them out. This means you can save some disk space and simply run them only when you need them. This also means your global variables will be less polluted.
有時,您只想使用一些CLI工具,但不想只是為了進行測試而全局安裝它們。 這意味著您可以節省一些磁盤空間,僅在需要它們時才運行它們。 這也意味著您的全局變量將減少污染。
直接從GitHub運行代碼 (Run code directly from GitHub)
This one’s pretty rad.
這個人很漂亮。
You can use npx to run any GitHub gists and repositories. Let’s focus on executing a GitHub gist because it’s easier to create one.
您可以使用npx運行任何GitHub要點和存儲庫。 讓我們專注于執行GitHub要點,因為它更容易創建。
The most basic script consists of the main JS file and a package.json
. After you’ve set up the files, all you have to do is run the npx with the link to that gist as shown in the image above.
最基本的腳本由主JS文件和package.json
。 設置完文件后,您所要做的就是運行帶有該gist鏈接的npx,如上圖所示。
Here you can find the code that I used for this example.
在這里,您可以找到我用于此示例的代碼。
Make sure you read carefully any script before you execute it to avoid serious problems that can occur due to malicious code.
在執行腳本之前,請確保仔細閱讀所有腳本,以避免由于惡意代碼而導致的嚴重問題。
測試不同的軟件包版本 (Test different package versions)
npx makes it extremely easy to test different versions of a Node.js package or module. To test this awesome feature, we’re going to locally install the create-react-app
package and test out an upcoming version.
npx使測試Node.js包或模塊的不同版本變得異常容易。 為了測試這個很棒的功能,我們將在本地安裝create-react-app
軟件包并測試即將發布的版本。
This will list some dist tags near the end of the output. Dist tags provide aliases for version numbers which makes it so much easier to type.
這將在輸出末尾附近列出一些dist標簽。 Dist標簽為版本號提供別名,這使得鍵入變得非常容易。
$ npm v create-react-app
Let’s use npx to try out the next
dist tag of create-react-app
which will create the app inside a sandbox directory.
讓我們使用npx嘗試next
create-react-app
dist標簽,該標簽將在沙盒目錄中創建該應用。
$ npx create-react-app@next sandbox
npx will temporarily install the next version of create-react-app
, and then it’ll execute to scaffold the app and install its dependencies.
npx將臨時安裝下一個版本的create-react-app
,然后它將執行以搭建應用程序并安裝其依賴項。
Once installed, we can navigate to the app like this:
安裝后,我們可以像這樣導航到該應用程序:
$ cd sandbox
and then start it with this command:
然后使用以下命令啟動它:
$ npm start
It will automatically open the React app in your default browser window.Now we have an app that runs on the next version of create-react-app
package!
它將自動在您的默認瀏覽器窗口中打開React應用程序。現在,我們有一個應用程序可以在下一版本的create-react-app
軟件包中運行!
結論 (Conclusion)
npx helps us avoid versioning, dependency issues and installing unnecessary packages that we just want to try out.
npx可以幫助我們避免版本控制,依賴性問題以及安裝我們僅想嘗試的不必要軟件包。
It also provides a clear and easy way of executing packages, commands, modules and even GitHub gists and repositories.
它還提供了一種執行包,命令,模塊,甚至是GitHub要點和存儲庫的清晰簡便的方法。
If you haven’t used npx before, now it is a good time to start!
如果您以前從未使用過npx,那么現在是開始的好時機!
This was originally posted on my blog.You can reach out and ask me anything on Twitter and Facebook.
這最初發布在我的博客上 。您可以在Twitter和Facebook 上與我聯系,問我任何問題。
翻譯自: https://www.freecodecamp.org/news/npm-vs-npx-whats-the-difference/
npx npm區別