Difference between npx and npm?
我刚刚开始学习React,Facebook通过提供以下现成的项目帮助简化初始设置。链接到GitHub上的Facebook帐户:https://github.com/facebook/create-react-app如果必须安装框架项目,则必须在命令行中键入
介绍NPX:一个NPM包运行程序
NPX comes bundled withNPM version5.2+
当可执行文件通过NPM包安装时,NPM链接到它们:
您应该阅读的文档
NPM:可以在特定项目上本地安装包:
1 | npm install some-package |
现在假设您希望nodejs从命令行执行该包:
1 | $ some-package |
以上都会失败。只有全局安装的包才能通过只键入其名称来执行。
要修复此问题并运行它,必须键入本地路径:
1 | $ ./node_modules/.bin/some-package |
您可以通过编辑您的
1 2 3 4 5 6 7 | { "name":"whatever", "version":"1.0.0", "scripts": { "some-package":"some-package" } } |
然后使用
1 | npm run some-package |
NPX:
1 | npx some-package |
1 | $ npx create-react-app my-app |
上面的示例将在命令运行的路径中生成一个
NPX是一个NPM包运行程序(X可能代表Execute)。典型的用途是临时下载和运行一个包或用于测试。
create react app是一个NPM包,在项目的生命周期中只运行一次。因此,最好使用NPX安装并在一个步骤中运行它。
如手册https://www.npmjs.com/package/npx所述,默认情况下,npx可以在路径中或从node_modules/.bin运行命令。
注:通过一些挖掘,我们可以发现create react app指向在节点环境中执行的javascript文件(可能指向Linux系统上的/usr/lib/node_modules/create react app/index.js)。这只是一个执行某些检查的全局工具。实际的设置是由react脚本完成的,该脚本的最新版本安装在项目中。更多信息请参考https://github.com/facebook/create-react-app。
NPX:
从https://www.futurehosting.com/blog/npx-makes-life-easier-for-node-developers-plus-node-viability-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.
用例:
- 您不希望在全局或本地安装软件包。
- 您没有全局安装的权限。
- 只想测试一些命令。
Syntax:
1 | npx [options] [-p|--package <package>] <command> [command-arg]... |
包是可选的:
1 2 3 | npx -p uglify-js uglifyjs --output app.min.js app.js common.js +----------------+ +--------------------------------------------+ package (optional) command, followed by arguments |
例如:
1 2 3 4 5 6 7 8 | 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 |
关于
- https://docs.npmjs.com/files/package.json_bin
- https://github.com/mishoo/uglifyjs2/blob/master/package.json l17
下面是一个NPX实际应用的例子:NPX Cowsay Hello
如果你把它输入到bash终端,你会看到结果。这样做的好处是NPX暂时安装了Cowsay。由于Cowsay没有永久性安装,因此没有包装污染。这对一次性包装很好,因为你想避免包装污染。
正如其他答案中提到的,NPX在需要安装包然后在运行之前进行配置的情况下也非常有用。例如,不要使用npm来安装,然后配置json.package文件,然后调用已配置的run命令,只需使用npx即可。一个真实的例子:NPX创建反应应用程序我的应用程序
NPM是一个包管理器,可以使用NPM安装node.js包。
NPX是执行node.js包的工具。
无论您是在全局还是在本地安装该软件包都不重要。NPX将临时安装并运行它。如果您配置package.json文件并将其包含在脚本部分中,NPM还可以运行包。
So remember this, if you want to check/run a node package quickly without installing locally or globally use NPX.