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 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派上用场的地方。
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非常有用的用例。
If you wish to execute a locally installed package, all you need to do is type:
如果要执行本地安装的软件包,只需输入:
$ npx your-package
npx will check whether
or
exists in $PATH
, or in the local project binaries, and if so it will execute it.
npx将检查$PATH
或本地项目二进制文件中是否存在
或
,如果存在,它将执行该命令。
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工具,但不想只是为了进行测试而全局安装它们。 这意味着您可以节省一些磁盘空间,仅在需要它们时才运行它们。 这也意味着您的全局变量将减少污染。
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.
在执行脚本之前,请确保仔细阅读所有脚本,以避免由于恶意代码而导致的严重问题。
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
软件包中运行!
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区别