npx npm区别_npm vs npx —有什么区别?

npx npm区别

If you’ve ever used Node.js, then you must have used npm for sure.

如果您曾经使用过Node.js ,那么一定要使用npm

npm (node package manager) is the dependency/package manager you get out of the box when you install Node.js. It provides a way for developers to install packages both globally and locally.

npm (节点程序包管理器)是您在安装Node.js时开箱即用的依赖项/程序包管理器。 它为开发人员提供了一种在全球和本地安装软件包的方法。

Sometimes you might want to take a look at a specific package and try out some commands. But you cannot do that without installing the dependencies in your local node_modules folder.

有时,您可能希望查看特定的软件包并尝试一些命令。 但是,如果不在本地node_modules文件夹中安装依赖项,就无法做到这node_modules

That’s where npx comes in.

那就是npx出现的地方。

In this article, we’re going to have a look at the differences between the npm and npx and learn how to get the best from both.

在本文中,我们将研究npmnpx之间的区别,并学习如何从这两者中获得最大的收益

First, let’s understand what npm actually is and what we can do with it.

首先,让我们了解npm实际上是什么以及我们可以使用它做什么。

npm包管理器 (npm the package manager)

npm is a couple of things. First and foremost, it is an online repository for the publishing of open-source Node.js projects.

npm是几件事情。 首先,它是一个在线资源库,用于发布开源Node.js项目。

Second, it is a CLI tool that aids you install those packages and manage their versions and dependencies. There are hundreds of thousands of Node.js libraries and applications on npm and many more are added every day.

其次,它是一个CLI工具,可帮助您安装这些软件包并管理其版本和依赖性。 npm上有成千上万个Node.js库和应用程序,并且每天都在增加。

npm by itself doesn’t run any packages. If you want to run a package using npm, you must specify that package in your package.json file.

npm本身不会运行任何软件包。 如果要使用npm运行软件包,则必须在package.json文件中指定该软件包。

When executables are installed via npm packages, npm creates links to them:

通过npm软件包安装可执行文件时,npm会创建指向它们的链接:

  • local installs have links created at the ./node_modules/.bin/ directory

    本地安装具有在./node_modules/.bin/目录中创建的链接

  • global installs have links created from the global bin/ directory (for example: /usr/local/bin on Linux or at %AppData%/npm on Windows)

    全局安装具有从全局bin/目录创建的链接(例如:Linux上为/usr/local/bin或Windows上为%AppData%/npm )

To execute a package with npm you either have to type the local path, like this:

要使用npm执行软件包,您必须输入本地路径,如下所示:

$ ./node_modules/.bin/your-package

or you can run a locally installed package by adding it into your package.json file in the scripts section, like this:

或者您可以通过将其添加到脚本部分的package.json文件中来运行本地安装的软件包,如下所示:

{"name": "your-application","version": "1.0.0","scripts": {"your-package": "your-package"}
}

Then you can run the script using npm run:

然后,您可以使用npm run运行脚本:

npm run your-package

You can see that running a package with plain npm requires quite a bit of ceremony.

您会看到,使用纯npm运行软件包需要大量的仪式。

Fortunately, this is where npx comes in handy.

幸运的是,这是npx派上用场的地方。

npx包运行器 (npx the package runner)

Since npm version 5.2.0 npx is pre-bundled with npm. So it’s pretty much a standard nowadays.

从npm 5.2.0版本开始, npx与npm预先捆绑在一起。 因此,这已成为当今的标准。

npx is also a CLI tool whose purpose is to make it easy to install and manage dependencies hosted in the npm registry.

npx还是一个CLI工具,其目的是使安装和管理npm注册表中托管的依赖项变得容易。

It’s now very easy to run any sort of Node.js based executable that you would normally install via npm.

现在,运行通常通过npm安装的各种基于Node.js的可执行文件非常容易。

You can run the following command to see if it is already installed for your current npm version:

您可以运行以下命令,以查看当前npm版本是否已安装该命令:

$ which npx

If it's not, you can install it like this:

如果不是,则可以这样安装:

$ npm install -g npx

Once you make sure you have it installed, let’s see a few of the use cases that make npx extremely helpful.

一旦确定已安装它,就让我们看一些使npx非常有用的用例。

轻松运行本地安装的软件包 (Run a locally installed package easily)

If you wish to execute a locally installed package, all you need to do is type:

如果要执行本地安装的软件包,只需输入:

$ npx your-package

npx will check whether <command> or <package> exists in $PATH, or in the local project binaries, and if so it will execute it.

npx将检查$PATH或本地项目二进制文件中是否存在<command><package> ,如果存在,它将执行该命令。

执行以前未安装的软件包 (Execute packages that are not previously installed)

Another major advantage is the ability to execute a package that wasn’t previously installed.

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

Sometimes you just want to use some CLI tools but you don’t want to install them globally just to test them out. This means you can save some disk space and simply run them only when you need them. This also means your global variables will be less polluted.

有时,您只想使用一些CLI工具,但不想只是为了进行测试而全局安装它们。 这意味着您可以节省一些磁盘空间,仅在需要它们时才运行它们。 这也意味着您的全局变量将减少污染。

直接从GitHub运行代码 (Run code directly from GitHub)

This one’s pretty rad.

这个人很漂亮。

You can use npx to run any GitHub gists and repositories. Let’s focus on executing a GitHub gist because it’s easier to create one.

您可以使用npx运行任何GitHub要点和存储库。 让我们专注于执行GitHub要点,因为它更容易创建。

The most basic script consists of the main JS file and a package.json. After you’ve set up the files, all you have to do is run the npx with the link to that gist as shown in the image above.

最基本的脚本由主JS文件和package.json 。 设置完文件后,您所要做的就是运行带有该gist链接的npx,如上图所示。

Here you can find the code that I used for this example.

在这里,您可以找到我用于此示例的代码。

Make sure you read carefully any script before you execute it to avoid serious problems that can occur due to malicious code.

在执行脚本之前,请确保仔细阅读所有脚本,以避免由于恶意代码而导致的严重问题。

测试不同的软件包版本 (Test different package versions)

npx makes it extremely easy to test different versions of a Node.js package or module. To test this awesome feature, we’re going to locally install the create-react-app package and test out an upcoming version.

npx使测试Node.js包或模块的不同版本变得异常容易。 为了测试这个很棒的功能,我们将在本地安装create-react-app软件包并测试即将发布的版本。

This will list some dist tags near the end of the output. Dist tags provide aliases for version numbers which makes it so much easier to type.

这将在输出末尾附近列出一些dist标签。 Dist标签为版本号提供别名,这使得键入变得非常容易。

$ npm v create-react-app

Let’s use npx to try out the next dist tag of create-react-app which will create the app inside a sandbox directory.

让我们使用npx尝试next create-react-app dist标签,该标签将在沙盒目录中创建该应用。

$ npx create-react-app@next sandbox

npx will temporarily install the next version of create-react-app, and then it’ll execute to scaffold the app and install its dependencies.

npx将临时安装下一个版本的create-react-app ,然后它将执行以搭建应用程序并安装其依赖项。

Once installed, we can navigate to the app like this:

安装后,我们可以像这样导航到该应用程序:

$ cd sandbox

and then start it with this command:

然后使用以下命令启动它:

$ npm start

It will automatically open the React app in your default browser window.Now we have an app that runs on the next version of create-react-app package!

它将自动在您的默认浏览器窗口中打开React应用程序。现在,我们有一个应用程序可以在下一版本的create-react-app软件包中运行!

结论 (Conclusion)

npx helps us avoid versioning, dependency issues and installing unnecessary packages that we just want to try out.

npx可以帮助我们避免版本控制,依赖性问题以及安装我们仅想尝试的不必要软件包。

It also provides a clear and easy way of executing packages, commands, modules and even GitHub gists and repositories.

它还提供了一种执行包,命令,模块,甚至是GitHub要点和存储库的清晰简便的方法。

If you haven’t used npx before, now it is a good time to start!

如果您以前从未使用过npx,那么现在是开始的好时机!

This was originally posted on my blog.You can reach out and ask me anything on Twitter and Facebook.

这最初发布在我的博客上 。您可以在Twitter和Facebook 上与我联系,问我任何问题。

翻译自: https://www.freecodecamp.org/news/npm-vs-npx-whats-the-difference/

npx npm区别

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390618.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

找出性能消耗是第一步,如何解决问题才是关键

作者最近刚接手一个新项目&#xff0c;在首页列表滑动时就感到有点不顺畅&#xff0c;特别是在滑动到有 ViewPager 部分的时候&#xff0c;如果是熟悉的项目&#xff0c;可能会第一时间会去检查代码&#xff0c;但前面说到这个是刚接手的项目&#xff0c;同时首页的代码逻辑比较…

bigquery_如何在BigQuery中进行文本相似性搜索和文档聚类

bigqueryBigQuery offers the ability to load a TensorFlow SavedModel and carry out predictions. This capability is a great way to add text-based similarity and clustering on top of your data warehouse.BigQuery可以加载TensorFlow SavedModel并执行预测。 此功能…

bzoj 1996: [Hnoi2010]chorus 合唱队

Description 为了在即将到来的晚会上有吏好的演出效果&#xff0c;作为AAA合唱队负责人的小A需要将合唱队的人根据他们的身高排出一个队形。假定合唱队一共N个人&#xff0c;第i个人的身髙为Hi米(1000<Hi<2000),并已知任何两个人的身高都不同。假定最终排出的队形是A 个人…

移动应用程序开发_什么是移动应用程序开发?

移动应用程序开发One of the most popular forms of coding in the last decade has been the creation of apps, or applications, that run on mobile devices.在过去的十年中&#xff0c;最流行的编码形式之一是创建在移动设备上运行的应用程序。 Today there are two main…

leetcode 1600. 皇位继承顺序(dfs)

题目 一个王国里住着国王、他的孩子们、他的孙子们等等。每一个时间点&#xff0c;这个家庭里有人出生也有人死亡。 这个王国有一个明确规定的皇位继承顺序&#xff0c;第一继承人总是国王自己。我们定义递归函数 Successor(x, curOrder) &#xff0c;给定一个人 x 和当前的继…

vlookup match_INDEX-MATCH — VLOOKUP功能的升级

vlookup match电子表格/索引匹配 (SPREADSHEETS / INDEX-MATCH) In a previous article, we discussed about how and when to use VLOOKUP functions and what are the issues that we might face while using them. This article, on the other hand, will take you to a jou…

java基础-BigDecimal类常用方法介绍

java基础-BigDecimal类常用方法介绍 作者&#xff1a;尹正杰 版权声明&#xff1a;原创作品&#xff0c;谢绝转载&#xff01;否则将追究法律责任。 一.BigDecimal类概述 我们知道浮点数的计算结果是未知的。原因是计算机二进制中&#xff0c;表示浮点数不精确造成的。这个时候…

节点对象转节点_节点流程对象说明

节点对象转节点The process object in Node.js is a global object that can be accessed inside any module without requiring it. There are very few global objects or properties provided in Node.js and process is one of them. It is an essential component in the …

PAT——1018. 锤子剪刀布

大家应该都会玩“锤子剪刀布”的游戏&#xff1a;两人同时给出手势&#xff0c;胜负规则如图所示&#xff1a; 现给出两人的交锋记录&#xff0c;请统计双方的胜、平、负次数&#xff0c;并且给出双方分别出什么手势的胜算最大。 输入格式&#xff1a; 输入第1行给出正整数N&am…

leetcode 1239. 串联字符串的最大长度

题目 二进制手表顶部有 4 个 LED 代表 小时&#xff08;0-11&#xff09;&#xff0c;底部的 6 个 LED 代表 分钟&#xff08;0-59&#xff09;。每个 LED 代表一个 0 或 1&#xff0c;最低位在右侧。 例如&#xff0c;下面的二进制手表读取 “3:25” 。 &#xff08;图源&am…

flask redis_在Flask应用程序中将Redis队列用于异步任务

flask redisBy: Content by Edward Krueger and Josh Farmer, and Douglas Franklin.作者&#xff1a; 爱德华克鲁格 ( Edward Krueger) 和 乔什法默 ( Josh Farmer )以及 道格拉斯富兰克林 ( Douglas Franklin)的内容 。 When building an application that performs time-co…

CentOS7下分布式文件系统FastDFS的安装 配置 (单节点)

背景 FastDFS是一个开源的轻量级分布式文件系统&#xff0c;为互联网量身定制&#xff0c;充分考虑了冗余备份、负载均衡、线性扩容等机制&#xff0c;并注重高可用、高性能等指标&#xff0c;解决了大容量存储和负载均衡的问题&#xff0c;特别适合以文件为载体的在线服务&…

如何修复会话固定漏洞_PHP安全漏洞:会话劫持,跨站点脚本,SQL注入以及如何修复它们...

如何修复会话固定漏洞PHP中的安全性 (Security in PHP) When writing PHP code it is very important to keep the following security vulnerabilities in mind to avoid writing insecure code.在编写PHP代码时&#xff0c;记住以下安全漏洞非常重要&#xff0c;以避免编写不…

剑指 Offer 38. 字符串的排列

题目 输入一个字符串&#xff0c;打印出该字符串中字符的所有排列。 你可以以任意顺序返回这个字符串数组&#xff0c;但里面不能有重复元素。 示例: 输入&#xff1a;s “abc” 输出&#xff1a;[“abc”,“acb”,“bac”,“bca”,“cab”,“cba”] 限制&#xff1a; 1…

前馈神经网络中的前馈_前馈神经网络在基于趋势的交易中的有效性(1)

前馈神经网络中的前馈This is a preliminary showcase of a collaborative research by Seouk Jun Kim (Daniel) and Sunmin Lee. You can find our contacts at the bottom of the article.这是 Seouk Jun Kim(Daniel) 和 Sunmin Lee 进行合作研究的初步展示 。 您可以在文章底…

解释什么是快速排序算法?_解释排序算法

解释什么是快速排序算法?Sorting algorithms are a set of instructions that take an array or list as an input and arrange the items into a particular order.排序算法是一组指令&#xff0c;这些指令采用数组或列表作为输入并将项目按特定顺序排列。 Sorts are most c…

SpringBoot自动化配置的注解开关原理

我们以一个最简单的例子来完成这个需求&#xff1a;定义一个注解EnableContentService&#xff0c;使用了这个注解的程序会自动注入ContentService这个bean。 Retention(RetentionPolicy.RUNTIME) Target(ElementType.TYPE) Import(ContentConfiguration.class) public interfa…

hadoop将消亡_数据科学家:适应还是消亡!

hadoop将消亡Harvard Business Review marked the boom of Data Scientists in their famous 2012 article “Data Scientist: Sexiest Job”, followed by untenable demand in the past decade. [3]《哈佛商业评论 》在2012年著名的文章“数据科学家&#xff1a;最性感的工作…

剑指 Offer 15. 二进制中1的个数 and leetcode 1905. 统计子岛屿

题目 请实现一个函数&#xff0c;输入一个整数&#xff08;以二进制串形式&#xff09;&#xff0c;输出该数二进制表示中 1 的个数。例如&#xff0c;把 9 表示成二进制是 1001&#xff0c;有 2 位是 1。因此&#xff0c;如果输入 9&#xff0c;则该函数输出 2。 示例 1&…

[转]kafka介绍

转自 https://www.cnblogs.com/hei12138/p/7805475.html kafka介绍1.1. 主要功能 根据官网的介绍&#xff0c;ApacheKafka是一个分布式流媒体平台&#xff0c;它主要有3种功能&#xff1a; 1&#xff1a;It lets you publish and subscribe to streams of records.发布和订阅消…