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.pswp.cn/news/390618.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390618.shtml
英文地址,請注明出處:http://en.pswp.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.發布和訂閱消…