Javascript npx 和 npm 的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50605219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:38:57  来源:igfitidea点击:

Difference between npx and npm?

javascriptnpmnpx

提问by Paresh Maniyar

I have just started learning React, and Facebook helps in simplifying the initial setup by providing the following ready-made project.

我刚开始学习 React,Facebook 通过提供以下现成的项目来帮助简化初始设置。

If I have to install the skeleton project I have to type npx create-react-app my-appin command-line.

如果我必须安装骨架项目,我必须npx create-react-app my-app在命令行中输入。

I was wondering why does the Facebook in Github have npx create-react-app my-apprather than npm create-react-app my-app?

我想知道为什么 Github 中的 Facebook 有npx create-react-app my-app而不是npm create-react-app my-app

回答by vsync

Introducing npx: an npm package runner

介绍 npx:一个 npm 包运行程序

NPM- Managespackages butdoesn't make life easy executingany.
NPX- A tool for executingNode packages.

NPM-管理包,不会让执行任何包变得容易。
NPX- 用于执行Node 包的工具。

NPXcomes bundled with NPMversion 5.2+

NPX附带NPM版本5.2+

NPMby itself does not simply run any package. it doesn't run any package in a matter of fact. If you want to run a package using NPM, you must specify that package in your package.jsonfile.

NPM本身并不简单地运行任何包。事实上,它不运行任何包。如果要使用 NPM 运行包,则必须在package.json文件中指定该包。

When executables are installed via NPM packages, NPM links to them:

当通过 NPM 包安装可执行文件时,NPM 会链接到它们:

  1. localinstalls have "links" created at ./node_modules/.bin/directory.
  2. globalinstalls have "links" created from the global bin/directory (e.g. /usr/local/bin) on Linux or at %AppData%/npmon Windows.
  1. 本地安装在./node_modules/.bin/目录中创建了“链接” 。
  2. 全局安装在 Linux 或Windows上具有从全局bin/目录(例如/usr/local/bin)创建的“链接” %AppData%/npm

Documentation you should read

您应该阅读的文档



NPM:

新产品管理:

One might install a package locally on a certain project:

人们可能会在某个项目上本地安装一个包:

npm install some-package

Now let's say you want NodeJS to execute that package from the command line:

现在假设您希望 NodeJS 从命令行执行该包:

$ some-package

The above will fail. Only globally installedpackages can be executed by typing their name only.

以上将失败。只有在全球安装的软件包可以通过输入自己的名字被执行

To fix this, and have it run, you must type the local path:

要解决此问题并运行它,您必须键入本地路径:

$ ./node_modules/.bin/some-package

You can technically run a locally installed package by editing your packages.jsonfile and adding that package in the scriptssection:

从技术上讲,您可以通过编辑packages.json文件并在以下scripts部分中添加该软件包来运行本地安装的软件包:

{
  "name": "whatever",
  "version": "1.0.0",
  "scripts": {
    "some-package": "some-package"
  }
}

Then run the script using npm run-script(or npm run):

然后使用npm run-script(或npm run)运行脚本:

npm run some-package


NPX:

NPX:

npxwill check whether <command>exists in $PATH, or in the local project binaries, and execute it. So, for the above example, if you wish to execute the locally-installed package some-packageall you need to do is type:

npx将检查是否<command>存在于$PATH,或在本地项目二进制文件中,并执行它。所以,对于上面的例子,如果你想执行本地安装的包,some-package你需要做的就是输入:

npx some-package

Another majoradvantage of npxis the ability to execute a package which wasn't previously installed:

的另一个主要优点npx是能够执行以前未安装的包:

$ npx create-react-app my-app

The above example will generate a reactapp boilerplate withinthe path the command had run in, and ensures that you always use the latest version of a generator or build tool without having to upgrade each time you're about to use it.

上面的例子会生成一个react应用样板的命令已在运行的路径,并确保你总是使用最新版本的发电机或构建工具,而无需每次你要使用它的时间来升级。



Related questions:

相关问题:

  1. How to use package installed locally in node_modules?
  2. NPM: how to source ./node_modules/.bin folder?
  3. How do you run a js file using npm scripts?
  1. 如何使用本地安装在 node_modules 中的包?
  2. NPM:如何获取 ./node_modules/.bin 文件夹?
  3. 如何使用 npm 脚本运行 js 文件?

回答by dww

npxis a npm package runner (x probably stands for eXecute). The typical use is to download and run a package temporarily or for trials.

npx是一个 npm 包运行程序(x 可能代表 eXecute)。典型用途是临时下载和运行包或用于试用。

create-react-appis an npm package that is expected to be run only once in a project's lifecycle. Hence, it is preferred to use npx to install and run it in a single step.

create-react-app是一个 npm 包,预计在项目的生命周期中只运行一次。因此,最好使用 npx 一步安装和运行它。

As mentioned in the man page https://www.npmjs.com/package/npx, npxcan run commands in the PATH or from node_modules/.bin by default.

正如在手册页提到https://www.npmjs.com/package/npxNPX可以运行路径或从node_modules / .bin文件默认情况下的命令。

Note:With some digging, we can find that create-react-app points to a Javascript file (possibly to /usr/lib/node_modules/create-react-app/index.js on Linux systems) that is executed within the node environment. This is simply a global tool that does some checks. The actual setup is done by react-scripts, whose latest version is installed in the project. Refer https://github.com/facebook/create-react-appfor more info.

注意:通过一些挖掘,我们可以发现 create-react-app 指向一个在 node 环境中执行的 Javascript 文件(在 Linux 系统上可能是 /usr/lib/node_modules/create-react-app/index.js) . 这只是一个进行一些检查的全局工具。实际设置由 react-scripts 完成,其最新版本已安装在项目中。有关更多信息,请参阅https://github.com/facebook/create-react-app

回答by cherankrish

NPM is a package manager, you can install node.js packages using NPM

NPM 是一个包管理器,你可以使用 NPM 安装 node.js 包

NPX is a tool to execute node.js packages.

NPX 是一个执行 node.js 包的工具。

It doesn't matter whether you installed that package globally or locally. NPX will temporarily install it and run it. NPM also can run packages if you configure a package.json file and include it in the script section.

无论您是全局安装还是本地安装该软件包都没有关系。NPX 将临时安装并运行它。如果您配置 package.json 文件并将其包含在脚本部分中,NPM 也可以运行包。

So remember this, if you want to check/run a node package quickly without installing locally or globally use NPX.

所以请记住这一点,如果您想快速检查/运行节点包而无需在本地或全局安装,请使用 NPX。

npM- Manager

np M- 经理

npX- Execute - easy to remember

np X- 执行 - 好记

回答by Venkat Ch

NPX:

NPX:

From https://www.futurehosting.com/blog/npx-makes-life-easier-for-node-developers-plus-node-vulnerability-news/:

来自https://www.futurehosting.com/blog/npx-makes-life-easier-for-node-developers-plus-node-vulnerability-news/

Web developers can have dozens of projects on their development machines, and each project has its own particular set of npm-installed dependencies. A few years back, the usual advice for dealing with CLI applications like Grunt or Gulp was to install them locally in each project and also globally so they could easily be run from the command line.

But installing globally caused as many problems as it solved. Projects may depend on different versions of command line tools, and polluting the operating system with lots of development-specific CLI tools isn't great either. Today, most developers prefer to install tools locally and leave it at that.

Local versions of tools allow developers to pull projects from GitHub without worrying about incompatibilities with globally installed versions of tools. NPM can just install local versions and you're good to go. But project specific installations aren't without their problems: how do you run the right version of the tool without specifying its exact location in the project or playing around with aliases?

That's the problem npx solves. A new tool included in NPM 5.2, npx is a small utility that's smart enough to run the right application when it's called from within a project.

If you wanted to run the project-local version of mocha, for example, you can run npx mocha inside the project and it will do what you expect.

A useful side benefit of npx is that it will automatically install npm packages that aren't already installed. So, as the tool's creator Kat Marchán points out, you can run npx benny-hill without having to deal with Benny Hill polluting the global environment.

If you want to take npx for a spin, update to the most recent version of npm.

Web 开发人员可以在他们的开发机器上拥有数十个项目,每个项目都有自己特定的一组 npm 安装的依赖项。几年前,处理 Grunt 或 Gulp 等 CLI 应用程序的通常建议是在每个项目中本地安装它们,也可以全局安装它们,以便可以轻松地从命令行运行它们。

但是在全球安装引起的问题和它解决的问题一样多。项目可能依赖于不同版本的命令行工具,使用大量特定于开发的 CLI 工具污染操作系统也不是很好。今天,大多数开发人员更喜欢在本地安装工具并保留它。

工具的本地版本允许开发人员从 GitHub 拉取项目,而不必担心与全球安装的工具版本不兼容。NPM 只需安装本地版本即可。但是特定于项目的安装并非没有问题:如何在不指定其在项目中的确切位置或使用别名的情况下运行正确版本的工具?

这就是 npx 解决的问题。NPM 5.2 中包含的一个新工具 npx 是一个小实用程序,它足够智能,可以在从项目中调用时运行正确的应用程序。

例如,如果您想运行项目本地版本的 mocha,您可以在项目中运行 npx mocha,它会按照您的预期运行。

npx 的一个有用的附带好处是它会自动安装尚未安装的 npm 包。因此,正如该工具的创建者 Kat Marchán 指出的那样,您可以运行 npx benny-hill 而不必处理 Benny Hill 污染全球环境。

如果您想尝试使用 npx,请更新到最新版本的 npm。

回答by Ninh Pham

npxruns a command of a package without installing it explicitly.

npx运行包的命令而不显式安装它。

Use cases:

用例:

  • You don't want to install packages neither globally nor locally.
  • You don't have permission to install it globally.
  • Just want to test some commands.
  • 您不想在全局或本地安装软件包。
  • 您无权全局安装它。
  • 只是想测试一些命令。

Syntax:

句法:

npx [options] [-p|--package <package>] <command> [command-arg]...

Package is optional:

套餐可选:

npx   -p uglify-js         uglifyjs --output app.min.js app.js common.js
      +----------------+   +--------------------------------------------+
      package (optional)   command, followed by arguments

For example:

例如:

Start a HTTP Server      : npx http-server
Lint code                : npx eslint ./src
                         # Run uglifyjs command in the package uglify-js
Minify JS                : npx -p uglify-js uglifyjs -o app.min.js app.js common.js
Minify CSS               : npx clean-css-cli -o style.min.css css/bootstrap.css style.css
Minify HTML              : npx html-minifier index-2.html -o index.html --remove-comments --collapse-whitespace
Scan for open ports      : npx evilscan 192.168.1.10 --port=10-9999
Cast video to Chromecast : npx castnow http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerFun.mp4

More about command:

更多关于command

回答by Yassine Qoraiche

Simple Definition:

简单定义:

npm- Javascript package manager

npm- Javascript 包管理器

npx- Execute npm package binaries

npx- 执行 npm 包二进制文件

回答by Risteard

Here's an example of NPX in action: npx cowsay hello

下面是 NPX 的示例: npx cowsay hello

If you type that into your bash terminal you'll see the result. The benefit of this is that npx has temporarily installed cowsay. There is no package pollution since cowsay is not permanently installed. This is great for one off packages where you want to avoid package pollution.

如果您在 bash 终端中输入它,您将看到结果。这样做的好处是npx临时安装了cowsay。由于 cowsay 不是永久安装的,因此没有包污染。这对于您想要避免包装污染的一次性包装非常有用。

As mentioned in other answers, npx is also very useful in cases where (with npm) the package needs to be installed then configured before running. E.g. instead of using npm to install and then configure the json.package file and then call the configured run command just use npx instead. A real example: npx create-react-app my-app

正如其他答案中提到的,npx 在(使用 npm)需要安装包然后在运行之前进行配置的情况下也非常有用。例如,不要使用 npm 安装然后配置 json.package 文件,然后调用配置的运行命令,只需使用 npx 即可。一个真实的例子: npx create-react-app my-app