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

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

问题描述

我刚刚开始学习React,Facebook通过提供准备就绪来帮助简化初始设置制作的项目.

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

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

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

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

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

推荐答案

介绍npx:一个npm包运行器

NPM-管理程序包,但并不能使执行变得容易.
NPX-用于执行节点程序包.

Introducing npx: an npm package runner

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

NPXNPM版本5.2+

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

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.

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

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

  1. 本地安装具有链接"在./node_modules/.bin/目录中创建.
  2. 全局安装具有链接"是从Linux上的全局bin/目录(例如/usr/local/bin)或Windows上的%AppData%/npm目录创建的.
  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.

> 您应阅读的文档

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

One might install a package locally on a certain project:

npm install some-package

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

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

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

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"
  }
}

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

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

npm run some-package


NPX:

npx将检查<command>是否存在于$PATH或本地项目二进制文件中,并执行它.因此,对于上面的示例,如果您希望执行本地安装的软件包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

npx的另一个主要优势是能够执行以前未安装的软件包:

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

$ npx create-react-app my-app

上面的示例将在命令运行的路径内 中生成一个react应用样板,并确保您始终使用最新版本的生成器或构建工具,而不必升级每个版本您将要使用它的时间.

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.

  1. 如何使用在node_modules中本地安装的软件包?
  2. NPM:如何获取./node_modules/.bin文件夹?
  3. 如何使用npm脚本运行js文件?
  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天全站免登陆