npx 和 npm 的区别? [英] Difference between npx and npm?

查看:27
本文介绍了npx 和 npm 的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习 React,Facebook 通过提供以下准备帮助简化了初始设置- 制作项目.

如果我必须安装骨架项目,我必须在命令行中输入 npx create-react-app my-app.

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

解决方案

介绍 npx:一个 npm 包运行器

NPM - 管理但是并不能使执行变得容易.
NPX - 执行 Node 包的工具.

<块引用>

NPXNPM 版本 5.2+

捆绑在一起

NPM 本身并不简单地运行任何包.事实上,它不运行任何包.如果要使用 NPM 运行包,则必须在 package.json 文件中指定该包.

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

  1. 本地安装有链接"在 ./node_modules/.bin/ 目录下创建.
  2. 全球安装有链接"从 Linux 上的全局 bin/ 目录(例如 /usr/local/bin)或 Windows 上的 %AppData%/npm 创建.

您应该阅读的文档


NPM:

一个人可能会在某个项目上本地安装一个包:

npm install some-package

现在假设您希望 NodeJS 从命令行执行该包:

$ some-package

以上将失败.只有全局安装的包可以通过输入它们的名称​​only来执行.

要解决此问题并让它运行,您必须输入本地路径:

$ ./node_modules/.bin/some-package

从技术上讲,您可以通过编辑 packages.json 文件并在 scripts 部分中添加该包来运行本地安装的包:

<代码>{名称":随便",版本":1.0.0",脚本":{一些包裹":一些包裹"}}

然后使用 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 命令在 package.json 文件的 script 部分可能会有所帮助,当不需要定义可能不常用的依赖项或任何其他原因时:

脚本":{开始":npx gulp@3.9.1",服务":npx http-server"}

调用:npm run serve


相关问题:

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

I have just started learning React, and Facebook helps in simplifying the initial setup by providing the following ready-made project.

If I have to install the skeleton project I have to type npx create-react-app my-app in command-line.

I was wondering why does the Facebook in Github have npx create-react-app my-app rather than npm create-react-app my-app?

解决方案

Introducing npx: an npm package runner

NPM - Manages packages but doesn't make life easy executing any.
NPX - A tool for executing Node packages.

NPX comes bundled with NPM version 5.2+

NPM by itself does not simply run any package. 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.

When executables are installed via NPM packages, NPM links to them:

  1. local installs have "links" created at ./node_modules/.bin/ directory.
  2. global installs have "links" created from the global bin/ directory (e.g. /usr/local/bin) on Linux or at %AppData%/npm on Windows.

Documentation you should read


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:

$ 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:

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

Then run the script using npm run-script (or npm run):

npm run some-package


NPX:

npx will check whether <command> exists in $PATH, or in the local project binaries, and execute it. So, for the above example, if you wish to execute the locally-installed package some-package all you need to do is type:

npx some-package

Another major advantage of npx is the ability to execute a package which wasn't previously installed:

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


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:

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

Call with: npm run serve


Related questions:

  1. How to use package installed locally in node_modules?
  2. NPM: how to source ./node_modules/.bin folder?
  3. How do you run a js file using npm scripts?

这篇关于npx 和 npm 的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆