{"id":1453,"date":"2021-11-30T15:18:48","date_gmt":"2021-11-30T14:18:48","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=1453"},"modified":"2021-11-30T15:18:48","modified_gmt":"2021-11-30T14:18:48","slug":"what-are-the-differences-between-npm-and-npx","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2021\/11\/30\/what-are-the-differences-between-npm-and-npx\/","title":{"rendered":"What are the differences between npm and npx ?"},"content":{"rendered":"\n<h4>What is NPM?<\/h4>\n\n\n\n<p>NPM stands for Node Package Manager. It comes pre-installed with Node.js. NPM is used to install Node.js packages to use them in our application. <em><strong>It makes it easier for developers to share and reuse open source code by enabling them to be installed as modules<\/strong><\/em>. Modules are JavaScript packages that you can install in your system using NPM. NPM helps to manage packages in your projects as dependencies.<\/p>\n\n\n\n<h4>What is NPX?<\/h4>\n\n\n\n<p><a href=\"https:\/\/www.npmjs.com\/package\/npx\">NPX<\/a>\u00a0is an NPM package executor. Initially, NPX was launched in<strong> July 2017<\/strong>.<\/p>\n\n\n\n<p>NPX was just an NPM package that could be installed like other NPM packages.<em><strong> Currently, NPX is bundled with NPM when you install the NPM version 5.2.0 or higher.<\/strong><\/em><\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>How does NPM treat Node.js packages?<\/h4>\n\n\n\n<p>It sets up modules such that Node.js can locate the packages and manage the dependencies of that application.<\/p>\n\n\n\n<p>When using NPM, there are two ways to install a package into your local computer.<\/p>\n\n\n\n<ul><li>Locally: When a package is installed locally, it is installed in\u00a0<span class=\"has-inline-color has-vivid-cyan-blue-color\"><code>\".\/node_modules\/.bin\/\"<\/code>\u00a0<\/span> of the local project directory.<\/li><li>Globally: A global package is installed in the user environment path.\u00a0<span class=\"has-inline-color has-vivid-cyan-blue-color\">&#8220;<code>\/usr\/local\/bin<\/code>&#8220;<\/span>\u00a0for Linux and\u00a0<span class=\"has-inline-color has-vivid-cyan-blue-color\">&#8220;<code>AppData%\/npm<\/code>&#8220;<\/span>\u00a0for Windows.<\/li><\/ul>\n\n\n\n<p>When you install executables using NPM, Node.js links them either from the local or global path. <em><strong>NPM does not execute a package directly.<\/strong><\/em><\/p>\n\n\n\n<p>To use and run an NPM installed package, you should specify the package in the\u00a0<code><span class=\"has-inline-color has-vivid-cyan-blue-color\">package.json<\/span><\/code>\u00a0file. The\u00a0<code>package.json<\/code>\u00a0file is created automatically when you initialize your Node.js project with\u00a0<span class=\"has-inline-color has-vivid-cyan-blue-color\"><em><strong><code>npm init -y<\/code> <\/strong><\/em><\/span>(the option -y , automatically answer &#8220;yes&#8221; to any prompts that npm might print on the command line..)<\/p>\n\n\n\n<p>To execute a locally installed package, it should be specified in the\u00a0<code>package.json<\/code>\u00a0scripts block as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\r\n    \"name\": \"Your app\",\r\n    \"versiuon\":  \"1.0.0\",\r\n    \"scripts\":  {\r\n            \"your-package\":  \"your-package-name\"\r\n     }\r\n}<\/code><\/pre>\n\n\n\n<p>Then, you can execute the package with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm run your-package-name<\/code><\/pre>\n\n\n\n<p><em><span class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>Alternativaly,<\/strong><\/span><\/em> you can execute the same package by typing the local path in a command-line tool.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/node_modules\/.bin\/your-package-name<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-red-color\"><strong>\u00a0Example<\/strong><\/span><\/h5>\n\n\n\n<p>Let\u2019s say you have installed\u00a0<a href=\"https:\/\/www.npmjs.com\/package\/eslint\">Eslint<\/a>, a tool for identifying and reporting on patterns found in ECMAScript\/JavaScript code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install eslint<\/code><\/pre>\n\n\n\n<p>To execute the package binaries, you have to point to the package path.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/node_modules\/.bin\/eslint --init<\/code><\/pre>\n\n\n\n<p>You should provide the file name as the argument to execute for the ESlint package.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/node_modules\/.bin\/eslint yourfile.js<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>NPX comes in to save a couple of keystrokes to make it easier to execute Node.js modules. It makes it easier to run and interact with executables hosted in the NPM registry and to execute NPM local binaries. It also comes with many added features.<\/p><\/blockquote>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>Using NPX<\/h4>\n\n\n\n<p>With NPX, you can run and execute packages without having to install them locally or globally.<\/p>\n\n\n\n<p>When running NPM executables with NPX, if a package is installed, NPX will search for the package binaries (either locally or globally) and then run the package.<\/p>\n\n\n\n<p>If the package was not previously installed, NPX will not install the package in your system; instead, it will create a temporary cache that will hold the package binaries. Once the execution is over, NPX will remove the installed cache binaries from the system.<\/p>\n\n\n\n<p>This way, your globals stays clean. This saves disk space and allows you to run a package only when it\u2019s needed. It also gives you the advantage of testing packages without having to install them.<\/p>\n\n\n\n<p>When using NPM, you have to remember to type the path of the package in the command, as explained in the Eslint case.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/node_modules\/.bin\/eslint yourfile.js<\/code><\/pre>\n\n\n\n<p>With NPX, you can have your local packages installed and use NPX commands (instead of adding the whole path) to execute the package binaries.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npx eslint yourfile.js<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>Why NPX over NPM scripts?<\/h4>\n\n\n\n<ul><li>No need to edit the\u00a0<code>package.json<\/code>\u00a0file with\u00a0<code>node_modules<\/code>\u00a0paths.<\/li><li>You can directly execute the tool from the command line.<\/li><li>Packages used by npx are not installed globally so you don\u00b4t have to care for the pollution for the long term.<\/li><li>With NPM, if you want to test packages, you have to download the packages and run them inside your project. This comes in handy when upgrading packages to newer versions. With NPX, you can determine if the new version is compatible with your project before deciding whether to update them.\u00a0<\/li><\/ul>\n\n\n\n<h5>Example:<\/h5>\n\n\n\n<p>\u00a0let\u2019s take a Node.js project, for instance where\u00a0<a href=\"https:\/\/www.npmjs.com\/package\/webpack\">Webpack<\/a>\u00a0is installed globally, and the currently installed version is 4.40.3. Webpack is used for bundling project assets. Check out this\u00a0<a href=\"https:\/\/www.section.io\/engineering-education\/webpack\/\">guide<\/a>\u00a0to learn more about configuring Webpack with your project.<\/p>\n\n\n\n<p> When configured, run<em><span class=\"has-inline-color has-pale-pink-color\">\u00a0<code>webpack<\/code>\u00a0<\/span><\/em>to get the command results.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"648\" height=\"201\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/11\/image-14.png\" alt=\"\" class=\"wp-image-1454\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/11\/image-14.png 648w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/11\/image-14-300x93.png 300w\" sizes=\"(max-width: 648px) 100vw, 648px\" \/><\/figure>\n\n\n\n<p>Let\u2019s try using NPX and run the webpack at version 5.11.1 to see what the difference would look like on the project. Run<em><span class=\"has-inline-color has-vivid-red-color\">\u00a0<code>npx webpack@5.11.1<\/code><\/span><\/em>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"618\" height=\"190\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/11\/image-15.png\" alt=\"\" class=\"wp-image-1455\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/11\/image-15.png 618w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/11\/image-15-300x92.png 300w\" sizes=\"(max-width: 618px) 100vw, 618px\" \/><\/figure>\n\n\n\n<p>Here, the results show that the new version will help you buddle your project further with a couple of KBs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is NPM? NPM stands for Node Package Manager. It comes pre-installed with Node.js. NPM is used to install Node.js packages to use them in our application. It makes it easier for developers to share and reuse open source code by enabling them to be installed as modules. Modules are JavaScript packages that you can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1456,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[145],"tags":[184,406],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1453"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=1453"}],"version-history":[{"count":1,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1453\/revisions"}],"predecessor-version":[{"id":1457,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1453\/revisions\/1457"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/1456"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=1453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=1453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=1453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}