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通过提供以下现成项目帮助简化初始设置。
Link to Facebook account on Github: https://github.com/facebook/create-react-app 链接到Github上的Facebook帐户: https//github.com/facebook/create-react-app
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 account in Github have npx create-react-app my-app rather than npm create-react-app my-app ? 我想知道为什么Github中的facebook帐户有npx create-react-app my-app而不是npm create-react-app my-app


#1楼

参考:https://stackoom.com/question/3QKjD/npx和npm之间的区别


#2楼

npx is a npm package runner (x probably stands for eXecute). npx是一个npm包运行器(x可能代表eXecute)。 The typical use is to download and run a package temporarily or for trials. 典型的用途是临时下载和运行包或进行试用。

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 man 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可以运行路径或从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


#3楼

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. 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. 所以,正如该工具的创造者KatMarchá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。


#4楼

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 - 用于执行节点包的工具。

NPX comes bundled with NPM version 5.2+ NPX捆绑了NPM 5.2+版本

NPM by itself does not simply run any package. NPM本身并不简单地运行任何包。 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.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: 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 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 runnpm run-script

npm run some-package

NPX: NPX:

npx will check whether <command> exists in $PATH , or in the local project binaries, and execute it. npx将检查$PATH或本地项目二进制文件中是否存在<command>并执行它。 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的命令已经运行的路径应用的样板,并确保你总是使用最新版本的发电机或无需在每次你要使用它的时间来升级构建工具。


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文件?

#5楼

Here's an example of NPX in action: npx cowsay hello 这是NPX的一个例子:npx cowsay你好

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在需要安装包然后在运行之前配置的情况下也非常有用。 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文件,然后调用配置的run命令,而不是使用npx。 A real example: npx create-react-app my-app 一个真实的例子:npx create-react-app my-app


#6楼

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. 只想测试一些命令。

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值