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

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

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


当前回答

npx是一个npm包运行器(x可能代表eXecute)。使用npx的一种常见方法是下载并临时运行一个包或进行试用。

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

正如主页https://www.npmjs.com/package/npx中提到的,默认情况下,npx可以在PATH或node_modules/.bin中运行命令。

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

其他回答

npx是一个npm包运行器(x可能代表eXecute)。使用npx的一种常见方法是下载并临时运行一个包或进行试用。

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

正如主页https://www.npmjs.com/package/npx中提到的,默认情况下,npx可以在PATH或node_modules/.bin中运行命令。

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

NPM =>是一个JS包管理器。

NPX =>是一个执行Node包和执行npm包二进制文件的工具。

很容易记住:

-npm代表MANAGER

-npx代表EXECUTE

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.

NPM与NPX的区别如下:

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

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

简单的答案是

NPX:用于执行任何节点包,而无需在我们的机器上安装包。

NPM:用于在我们的机器上安装任何node js包。当我们使用NPM安装任何包时,我们可以使用"require("package-name')"。但是我们在使用NPX的时候不能导入这个包。

示例:你应该运行npm i axios 在本例中,您正在本地机器上安装axios包

NPX create-react-app 'app-name' 在这里,你直接在你的机器上执行create-react-app包,而不安装它的文件。