简体   繁体   English

npx 和 npm 的区别?

[英]Difference between npx and npm?

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-app in command-line.如果我必须安装框架项目,我必须在命令行中输入npx create-react-app my-app

I was wondering why does the Facebook in Github have npx create-react-app my-app rather than npm create-react-app my-app ?我想知道为什么 Github 中的 Facebook 有 npx npx create-react-app my-app而不是npm create-react-app my-app

Introducing npx: an npm package runner 介绍 npx:一个 npm 包运行器

NPM - Manages packages but doesn't make life easy executing any. NPM -管理,但执行起来并不容易。
NPX - A tool for executing Node packages. NPX - 用于执行Node 包的工具。

NPX comes bundled with NPM version 5.2+ NPXNPM版本5.2+捆绑在一起

NPM by itself does not simply run any package. NPM本身并不简单地运行任何包。 It doesn't run any package as a matter of fact.事实上,它不运行任何包。 If you want to run a package using NPM, you must specify that package in your package.json file.如果要使用 NPM 运行包,则必须在package.json文件中指定该包。

When executables are installed via NPM packages, NPM links to them:当通过 NPM 包安装可执行文件时,NPM 链接到它们:

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

Documentation you should read您应该阅读的文档


NPM:新PM:

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 installed packages 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.json file and adding that package in the scripts section:您可以通过编辑您的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: NP:

npx will check whether <command> exists in $PATH , or in the local project binaries, and execute it. npx将检查<command>是否存在于$PATH或本地项目二进制文件中,并执行它。 So, for the above example, if you wish to execute the locally-installed package some-package all you need to do is type:所以,对于上面的例子,如果你想执行本地安装的包some-package你需要做的就是输入:

npx some-package

Another major advantage of npx is the ability to execute a package which wasn't previously installed: npx的另一个主要优点是能够执行以前未安装的包:

$ npx create-react-app my-app

The above example will generate a react app boilerplate within the 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 app 样板,并确保您始终使用最新版本的生成器或构建工具,而无需在每次要使用它时进行升级。


Use-Case Example:用例示例:

npx command may be helpful in the script section of a package.json file, when it is unwanted to define a dependency which might not be commonly used or any other reason: npx命令在package.json文件的script部分可能会有所帮助,当不需要定义可能不常用或任何其他原因的依赖项时:

"scripts": {
    "start": "npx gulp@3.9.1",
    "serve": "npx http-server"
}

Call with: npm run serve调用: npm run serve


Related questions:相关问题:

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

npx is a npm package runner (x probably stands for eXecute). npx是一个 npm 包运行器(x 可能代表 eXecute)。 One common way to use npx is to download and run a package temporarily or for trials.使用npx的一种常见方法是临时下载并运行一个包或进行试用。

create-react-app is an npm package that is expected to be run only once in a project's lifecycle. create-react-app是一个 npm 包,预计在项目的生命周期中只运行一次。 Hence, it is preferred to use npx to install and run it in a single step.因此,最好使用 npx 一步安装和运行它。

As mentioned in the main page https://www.npmjs.com/package/npx , npx can run commands in the PATH or from node_modules/.bin by default.正如主页https://www.npmjs.com/package/npx中提到的,npx 默认可以在 PATH 或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.注意:通过一些挖掘,我们可以发现 create-react-app 指向一个在节点环境中执行的 Javascript 文件(在 Linux 系统上可能指向 /usr/lib/node_modules/create-react-app/index.js) . 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.实际设置由 react-scripts 完成,其最新版本已安装在项目中。 Refer https://github.com/facebook/create-react-app for more info.有关更多信息,请参阅https://github.com/facebook/create-react-app

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. NPX 将临时安装并运行它。 NPM also can run packages if you configure a package.json file and include it in the script section.如果您配置 package.json 文件并将其包含在脚本部分中,NPM 也可以运行包。

So remember this, if you want to check/run a node package quickly without installing locally or globally use NPX.所以请记住这一点,如果您想快速检查/运行节点包而不在本地或全局安装,请使用 NPX。

np M - Manager np M - 经理

np X - Execute - easy to remember np X - 执行 - 容易记住

npm - Package manager for JavaScript, just like: pip (Python), Maven (Java), NuGet (.NET), Composer (PHP), RubyGems (Ruby), ... npm - JavaScript 的包管理器,就像: pip (Python)、 Maven (Java)、 NuGet (.NET)、 Composer (PHP)、 RubyGems (Ruby)...

npx - runs 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.只是想测试一些命令。
  • Sometime, you want to have a script command (generate, convert something, ...) in package.json to execute something without installing these packages as project's dependencies.有时,您希望在package.json中有一个脚本命令(生成、转换某些内容,...)来执行某些内容,而无需将这些包安装为项目的依赖项。

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

NPX: NP:

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

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.项目可能依赖于不同版本的命令行工具,使用大量开发特定的 CLI 工具污染操作系统也不是很好。 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.本地版本的工具允许开发人员从 GitHub 拉取项目,而不必担心与全球安装的工具版本不兼容。 NPM can just install local versions and you're good to go. NPM 可以只安装本地版本,你就可以开始了。 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.这就是 npx 解决的问题。 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. NPM 5.2 中包含一个新工具,npx 是一个小型实用程序,它足够智能,可以在从项目中调用它时运行正确的应用程序。

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.例如,如果你想运行本地版本的 mocha,你可以在项目中运行 npx mocha,它会按照你的期望运行。

A useful side benefit of npx is that it will automatically install npm packages that aren't already installed. npx 的一个有用的附带好处是它会自动安装尚未安装的 npm 包。 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.因此,正如该工具的创建者 Kat Marchán 所指出的,您可以运行 npx benny-hill 而无需处理 Benny Hill 污染全球环境的问题。

If you want to take npx for a spin, update to the most recent version of npm.如果您想试用 npx,请更新到最新版本的 npm。

Simple Definition:简单定义:

npm - Javascript package manager npm - Javascript 包管理器

npx - Execute npm package binaries npx - 执行 npm 包二进制文件

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.如果你在你的 bash 终端中输入它,你会看到结果。 The benefit of this is that npx has temporarily installed cowsay.这样做的好处是 npx 已经临时安装了 cowsay。 There is no package pollution since cowsay is not permanently installed.由于没有永久安装cowsay,因此没有包装污染。 This is great for one off packages where you want to avoid package pollution.这对于您想要避免包装污染的一次性包装非常有用。

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.正如其他答案中提到的,npx 在需要安装(使用 npm)包然后在运行之前进行配置的情况下也非常有用。 Eg instead of using npm to install and then configure the json.package file and then call the configured run command just use npx instead.例如,不要使用 npm 安装然后配置 json.package 文件,然后调用配置的运行命令,只需使用 npx 代替。 A real example: npx create-react-app my-app一个真实的例子:npx create-react-app my-app

NPM => Is a JS package manager. NPM => 是一个 JS 包管理器。

NPX => Is a tool for executing Node packages and execute npm package binaries. NPX => 是一个执行 Node 包和执行 npm 包二进制文件的工具。

It is easy to remember:很容易记住:

-np m stands for M ANAGER -np m代表M ANAGER

-np x stands for E X ECUTE -np x代表 E X ECUTE

NPM : NPM stands for Node Package Manager and is the default package manager for Node.js. NPM :NPM 代表 Node Package Manager,是 Node.js 的默认包管理器。 It was developed by Isaac Z. Schlueter and was originally released on January 12, 2010. It is entirely written in JavaScript.它由 Isaac Z. Schlueter 开发,最初于 2010 年 1 月 12 日发布。它完全用 JavaScript 编写。 It consists of a command-line client npm which manages all node.js packages and modules.它由一个命令行客户端 npm 组成,它管理所有 node.js 包和模块。 When node.js is installed, it is included in the installation.安装 node.js 时,它包含在安装中。

npm run your-package-name

NPX is a tool that use to execute packages. NPX是一个用来执行包的工具。

NPX is an acronym for Node Package Execute The NPX package comes with npm, so when you install npm above 5.2.0, NPX will be installed automatically. NPX 是 Node Package Execute 的缩写 NPX 包是 npm 自带的,所以当你在 5.2.0 以上安装 npm 时,会自动安装 NPX。

It is an npm package runner that can execute any package that you want from the npm registry without even installing that package.它是一个 npm 包运行程序,可以从 npm 注册表执行您想要的任何包,甚至无需安装该包。 The npx is useful during a single time use package. npx 在单次使用包中很有用。 If you have installed npm below 5.2.0 then npx is not installed in your system.如果你安装了低于 5.2.0 的 npm,那么你的系统中没有安装 npx。

Run the following command to determine if npx is installed:运行以下命令以确定是否安装了 npx:

npx -v

The following command can be run if npx is not installed.如果未安装 npx,则可以运行以下命令。

npm install -g npx

Use npx to execute the package:使用 npx 执行包:

npx your-package-name

NPM 与 NPX

Simplest Definition:最简单的定义:

NPX NPX

The npx stands for Node Package Execute and it comes with the npm, when you installed npm above 5.2.0 version then automatically npx will installed. npx 代表 Node Package Execute,它与 npm 一起提供,当您安装 npm 以上 5.2.0 版本时,将自动安装 npx。 It is an npm package runner that can execute any package that you want from the npm registry without even installing that package.它是一个 npm 包运行程序,可以从 npm 注册表执行您想要的任何包,甚至无需安装该包。

NPM新PM

npm is a package manager for the JavaScript programming language maintained by npm, Inc. npm is the default package manager for the JavaScript runtime environment Node.js. npm 是由 npm, Inc 维护的 JavaScript 编程语言的包管理器。npm 是 JavaScript 运行时环境 Node.js 的默认包管理器。 It consists of a command line client, also called npm, and an online database of public and paid-for private packages它由一个命令行客户端(也称为 npm)和一个公共和付费私人包的在线数据库组成

Simple answer is like简单的答案就像

NPX: is used to execute any node package without installing the package on our machine. NPX:用于执行任何节点包,无需在我们的机器上安装包。

NPM: is used to install any node js package in our machine. NPM:用于在我们的机器上安装任何 node js 包。 We can use " require("package-name') " when we install any package using NPM. but we can not import the package when we use NPX.当我们使用 NPM 安装任何包时,我们可以使用 " require("package-name') "。但是当我们使用 NPX 时,我们不能导入包。

Example: You should run npm i axios in this case you are installing axios package in your local machine示例:您应该运行 npm i axios 在这种情况下,您正在本地机器上安装 axios 包

and npx create-react-app 'app-name' here you are executing the create-react-app package directly on your machine without installing it's files.和 npx create-react-app 'app-name' 在这里你直接在你的机器上执行create-react-app包而不安装它的文件。

NPM stands for Node Package Manager . NPM代表节点包管理器 It comes pre-installed with Node.js.它预装了 Node.js。 NPM helps to manage packages in your projects as dependencies. NPM 有助于将项目中的包作为依赖项进行管理。

When using NPM, there are two ways to install a package into your local computer.使用 NPM 时,有两种方法可以将包安装到本地计算机。

  1. Locally : When a package is installed locally, it is installed in ./node_modules/.bin/ of the local project directory. Locally :本地安装包时,安装在本地项目目录的 ./node_modules/.bin/ 中。

  2. Globally : A global package is installed in the user environment path.全局:在用户环境路径中安装一个全局包。 /usr/local/bin for Linux and AppData%/npm for Windows. /usr/local/bin 用于 Linux 和 AppData%/npm 用于 Windows。

To execute a locally installed package, it should be specified in the package.json scripts block as shown below.要执行本地安装的包,应该在 package.json 脚本块中指定它,如下所示。

"scripts": {
    "your-package":  "your-package-name"
}

Then, you can execute the package with:然后,您可以使用以下命令执行包:

npm run your-package-name

NPX is an NPM package executor . NPX是一个NPM 包执行器。 Currently, NPX is bundled with NPM when you install the NPM version 5.2.0 or higher.目前,当您安装 NPM 5.2.0或更高版本时,NPX 与 NPM 捆绑在一起。

Why NPX over NPM?为什么 NPX 优于 NPM?

  • No need to edit the package.json file with node_modules paths.无需使用 node_modules 路径编辑 package.json 文件。

  • You can directly execute the tool from the command line.您可以直接从命令行执行该工具。

The differences between NPM and NPX are as below: NPM和NPX的区别如下:

i) NPM is used to install the packages while NPX is used to execute the packages. i) NPM 用于安装包,而 NPX 用于执行包。

ii) Due to npm the packages installed have to be taken care of since it's installed globally while the packages used by npx don't need to be taken care of as they are not installed globally. ii) 由于 npm,安装的包必须得到照顾,因为它是全局安装的,而 npx 使用的包不需要照顾,因为它们不是全局安装的。

NPX 是用于在新项目中创建和执行某些功能的工具 NPM 是包含所有库的包管理器

NPM stands for Node Package Manager . NPM代表Node Package Manager NPM is Node.JS's default package manager. NPM 是 Node.JS 默认的 package 管理器。 It's written in Javascript. The role of NPM is to manage the package and modules of node.js.写在Javascript。NPM的作用是管理package和node.js的模块。

NPX stands for Node Package Execute . NPX代表Node Package Execute NPX comes with npm, when npm is installed above the 5.2.0 version, it gets installed automatically. NPX自带npm,当npm安装在5.2.0以上版本时,会自动安装。 NPX is an npm package runner and its role is to execute the package from the registry without even installing that package. NPX 是一个 npm package 运行程序,它的作用是从注册表中执行 package,甚至不安装 package。

Now, the differences between NPM and NPX are as below:现在,NPM和NPX的区别如下:

i) NPM is used to install the packages while NPX is used to execute the packages. i) NPM 用于安装包,而 NPX 用于执行包。

ii) Due to npm the packages installed have to be taken care of since it's installed globally while the packages which are used by npx don't need to be taken care of as they are not installed globally. ii) 由于 npm 的缘故,必须注意安装的包,因为它是全局安装的,而 npx 使用的包不需要注意,因为它们不是全局安装的。

Here's an example of what your app creation might look like using npx这是使用npx创建应用程序的示例

npx create-react-app project-name --template all npx create-react-app 项目名称 --template all

简单地说,npm 是节点包管理器,npx 是运行 npm 包的可执行版本

npm is a tool that use to install packages and npx is a tool that use to execute packages. npm 是用于安装包的工具,npx 是用于执行包的工具。 npm-If you wish to run package through npm then you have to specify that package in your package.json and install it locally. npm - 如果您希望通过 npm 运行包,那么您必须在 package.json 中指定该包并在本地安装它。 npx-A package can be executable without installing the package. npx-A 包可以在不安装包的情况下执行。 It is an npm package runner so if any packages aren't already installed it will install them automatically.它是一个 npm 包运行器,因此如果尚未安装任何包,它将自动安装它们。

npm - package manager npm - package 经理

npx - Execute npm package npx - 执行 npm package

This is a difference with it.这是与它的区别。

NPM vs. NPX NPM 与 NPX

NPM stands for the Node Package Manager. NPM 代表节点 Package 管理器。 A text based program for Nodejs package management.一个基于文本的 Nodejs package 管理程序。

While NPX is a Node Package Runner.而 NPX 是一个节点 Package Runner。 Its function is to execute the Nodejs package它的function是执行Nodejs package

NPX will execute binary files from the Nodejs package, both installed and not. NPX 将从 Nodejs package 执行二进制文件,包括已安装和未安装的。

Even NPX can also help us use certain versions of Nodejs without having to use nvm (node.js version management), nave (node.js virtual environment), and nvm (node.js version management).甚至 NPX 也可以帮助我们使用某些版本的 Nodejs,而无需使用 nvm(node.js 版本管理)、nave(node.js 虚拟环境)和 nvm(node.js 版本管理)。

Here is the simple definition.这是简单的定义。 NPM is a package manager, you can install node.js packages using NPM NPM是 package 管理员,您可以使用 NPM 安装 node.js 包

NPX is a tool to execute node.js packages. NPX是一个执行 node.js 个包的工具。

另一方面,npm 是包管理器或安装程序 npx 使用的包不是全局安装的,因此您必须长期无忧无虑地污染。

If you use npm 5.1 or earlier , you can't use npx. 如果您使用npm 5.1或更早版本 ,则无法使用npx。 Instead, install create-react-app globally: 而是全局安装create-react-app:

npm install -g create-react-app npm install -g create-react-app

Now you can run: 现在你可以运行:

create-react-app my-app create-react-app my-app

Actually, I tried many ways to solve this and failed but finally removing/deleting yarn globally solves the problem实际上,我尝试了很多方法来解决这个问题,但都失败了,但最终全局删除/删除纱线解决了这个问题

just type this command in your commandline terminal:只需在命令行终端中键入此命令:

npm uninstall -g yarn npm 卸载 -g 纱线

And then run the command below to install the react starter project然后运行下面的命令来安装 react starter 项目

npx create-react-app npx 创建反应应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM