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

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

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


当前回答

NPM代表节点包管理器。 它预装在Node.js中。NPM有助于将项目中的包作为依赖项来管理。

在使用NPM时,有两种方法将包安装到本地计算机中。

本地:当一个包在本地安装时,它被安装在 ./node_modules/.bin/ 全局:在用户环境中安装全局包 路径。Linux为“/usr/local/bin”,Windows为“AppData%/npm”。

要执行本地安装的包,应该在包中指定。Json脚本块如下所示。

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

然后,你可以执行这个包:

npm run your-package-name

NPX是一个NPM包执行器。 目前,当你安装NPM 5.2.0或更高版本时,NPX与NPM捆绑在一起。

为什么NPX胜过NPM?

不需要编辑包。带有node_modules路径的Json文件。 您可以直接从命令行执行该工具。

其他回答

NPM代表节点包管理器。NPM是Node.JS的默认包管理器。它是用Javascript写的。NPM的作用是管理node.js的包和模块。

NPX代表节点包执行。NPX附带了npm,当npm安装在5.2.0版本之上时,它会自动安装。NPX是一个npm包运行器,它的作用是从注册表执行包,甚至不需要安装包。

现在NPM和NPX的区别如下:

i) NPM用于安装包,NPX用于执行包。

ii)由于npm的缘故,安装的包必须被照顾,因为它是全局安装的,而npx使用的包不需要被照顾,因为它们不是全局安装的。

介绍npx:一个npm包运行器

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

NPX与NPM 5.2+版本捆绑在一起

NPM本身不能简单地运行任何包。事实上,它不运行任何包。如果你想使用NPM运行一个包,你必须在你的包中指定这个包。json文件。

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

本地安装在。/node_modules/.bin/目录下创建了“链接”。 在Linux上的全局bin/目录(例如/usr/local/bin)或Windows上的%AppData%/npm中创建了“链接”。

你应该阅读的文档


NPM:

你可以在某个项目上本地安装一个包:

npm install some-package

现在假设你想让NodeJS从命令行执行这个包:

$ some-package

上述操作将失败。只有全局安装的包可以通过只输入它们的名称来执行。

要修复这个问题,并让它运行,你必须输入本地路径:

$ ./node_modules/.bin/some-package

从技术上讲,您可以通过编辑您的包来运行本地安装的包。Json文件,并将该包添加到脚本部分:

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

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

npm run some-package

NPX:

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

npx some-package

npx的另一个主要优势是能够执行之前没有安装的包:

$ npx create-react-app my-app

上面的例子将在命令运行的路径中生成一个react应用样板,并确保您总是使用最新版本的生成器或构建工具,而不必在每次使用它时升级。


用例的例子:

NPX命令在包的脚本部分可能会有帮助。json文件, 当不需要定义一个可能不常用或出于其他原因的依赖项时:

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

调用:npm运行服务


相关问题:

如何使用本地安装在node_modules中的包? NPM:如何source ./node_modules/.bin文件夹? 如何使用npm脚本运行js文件?

这里有一个NPX的例子:NPX cowsay hello

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

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

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

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

无论您是在全局还是本地安装该包。NPX将临时安装并运行它。如果你配置了一个包,NPM也可以运行一个包。Json文件,并包括在脚本部分。

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

npM -经理

npX -执行-容易记住

NPX:

从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.