{"id":1851,"date":"2023-02-09T07:58:48","date_gmt":"2023-02-09T07:58:48","guid":{"rendered":"https:\/\/matob.web.id\/news\/?p=1851"},"modified":"2023-02-09T07:58:48","modified_gmt":"2023-02-09T07:58:48","slug":"learn-node-js-what-is-npx-and-whats-the-difference-with-npm","status":"publish","type":"post","link":"https:\/\/matob.web.id\/news\/learn-node-js-what-is-npx-and-whats-the-difference-with-npm\/","title":{"rendered":"Learn Node.js : What is NPX? and What&#8217;s the Difference with NPM?"},"content":{"rendered":"<p>When <a href=\"https:\/\/matob.web.id\/\">Matob<\/a> first saw <span style=\"color: #ff6600;\">npx<\/span>, I thought it was typo from <span style=\"color: #ff6600;\">npm<\/span>. Apparently not\u2026<\/p>\n<p>NPM and NPX are two tools that have different functions. What are the differences?<\/p>\n<p>&#8230; and when should we use NPX?<\/p>\n<p>Let&#8217;s discuss?<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-236\" src=\"https:\/\/matob.web.id\/random\/wp-content\/uploads\/sites\/2\/2019\/08\/npx.png\" alt=\"\" width=\"579\" height=\"317\" title=\"\"><\/p>\n<h2>NPM vs NPX<\/h2>\n<p>NPM stands for <em>Node Package Manager<\/em>. A text based program for Nodejs package management.<\/p>\n<p>Whereas NPX is a <em>Node Package Runner<\/em>. Its function is to execute the Nodejs package.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-234\" src=\"https:\/\/matob.web.id\/random\/wp-content\/uploads\/sites\/2\/2019\/08\/npm-npx.png\" alt=\"npm-npx\" width=\"578\" height=\"319\" title=\"\"><\/p>\n<p>NPX will execute binary files from the Nodejs package, both those that are already installed and those that are not.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-237\" src=\"https:\/\/matob.web.id\/random\/wp-content\/uploads\/sites\/2\/2019\/08\/npx-demo.gif\" alt=\"\" width=\"493\" height=\"230\" title=\"\"><\/p>\n<p>NPX can even help us to use certain versions of Nodejs without having to use <span style=\"color: #ff9900;\">nvm<\/span> (nodejs version management), <span style=\"color: #ff9900;\">nave<\/span> (nodejs vritual environment), and <span style=\"color: #ff9900;\">n<\/span> (nodejs version management).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-238\" src=\"https:\/\/matob.web.id\/random\/wp-content\/uploads\/sites\/2\/2019\/08\/npx-nodejs.png\" alt=\"\" width=\"500\" height=\"404\" title=\"\"><\/p>\n<h2>NPX installation<\/h2>\n<p>NPX was added to NPM version 5.2.0. So, if you use this version of NPM, you don&#8217;t need to install npx.<\/p>\n<p>But if your computer isn&#8217;t installed, you can install it with the command:<\/p>\n<pre>[sudo] npm install -g npx<\/pre>\n<p>Use <span style=\"color: #ff6600;\">sudo<\/span> if you install Nodejs in the root directory.<\/p>\n<h2>How to use NPX<\/h2>\n<p>How to use NPX is almost the same as NPM.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-239\" src=\"https:\/\/matob.web.id\/random\/wp-content\/uploads\/sites\/2\/2019\/08\/npx-use.png\" alt=\"\" width=\"684\" height=\"291\" title=\"\"><\/p>\n<p>For a list of other options and arguments, you can see with the help of <span style=\"color: #ff6600;\">npx<\/span>.<\/p>\n<p>Please type the command:<\/p>\n<pre>npx --help<\/pre>\n<p>To see help.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-240\" src=\"https:\/\/matob.web.id\/random\/wp-content\/uploads\/sites\/2\/2019\/08\/npx-help.png\" alt=\"\" width=\"750\" height=\"503\" title=\"\"><\/p>\n<p>Let&#8217;s try using npx &#8230;<\/p>\n<pre>npx happy-birthday -u \"Petani Kode\"<\/pre>\n<p>Note, <span style=\"color: #ff6600;\">happy-birthday<\/span> is the name of the package we will execute. Then <span style=\"color: #ff6600;\">-u &#8220;Petani Kode&#8221;<\/span> is the argument for the happy-birthday package.<\/p>\n<p>We can get this argument by reading the readme from this package on <a href=\"https:\/\/www.npmjs.com\/package\/happy-birthday\" target=\"_blank\" rel=\"noopener\">the npmjs website<\/a>.<\/p>\n<p><span style=\"color: #ff6600;\">Happy-birthday<\/span> package is a package that functions to display the text <span style=\"color: #ff6600;\">&#8220;Happy birthday&#8221;<\/span> in various languages.<\/p>\n<p>The result:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-241\" src=\"https:\/\/matob.web.id\/random\/wp-content\/uploads\/sites\/2\/2019\/08\/happy-birthday.png\" alt=\"\" width=\"750\" height=\"503\" title=\"\"><\/p>\n<h2>When Should We Use NPX?<\/h2>\n<p>We can use NPX for several cases like this:<\/p>\n<h3>1. When You Want Execution Only Once<\/h3>\n<p>We might only need a nodejs package to be executed once.<\/p>\n<p>Example:<\/p>\n<p>Package <span style=\"color: #ff6600;\">create-react-app<\/span> to create the React application project.<\/p>\n<p>Then we can use the command:<\/p>\n<pre>npx create-react-app nama-project<\/pre>\n<h3>2. When I Want to Execute a Script from Gist<\/h3>\n<p>We can use NPX to execute scripts from <span style=\"color: #ff9900;\">Gist<\/span>.<\/p>\n<p>Gist is a Github service for storing scripts like <span style=\"color: #ff9900;\">pastebin<\/span>.<\/p>\n<p>Let&#8217;s try executing the script from Gist with NPX.<\/p>\n<p>I will use <span style=\"color: #ff6600;\"><a style=\"color: #ff6600;\" href=\"https:\/\/gist.github.com\/ardianta\/df9a4c8be44b5d5f4bb1b67cdd13f4ea\" target=\"_blank\" rel=\"noopener\">a script that has been prepared<\/a><\/span>.<\/p>\n<pre>#!\/usr\/bin\/env node\r\n\r\nfor(var i = 0; i &lt; 10; i++){\r\nconsole.log(\"Hello World!\");\r\n}<\/pre>\n<pre>{\r\n\"name\": \"hello-world\",\r\n\"version\": \"0.0.0\",\r\n\"bin\": \".\/hello-world.js\"\r\n}<\/pre>\n<p>Try typing the following command:<\/p>\n<pre>npx https:\/\/gist.github.com\/ardianta\/df9a4c8be44b5d5f4bb1b67cdd13f4ea<\/pre>\n<p>Then the result:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-242\" src=\"https:\/\/matob.web.id\/random\/wp-content\/uploads\/sites\/2\/2019\/08\/gist-result.png\" alt=\"\" width=\"750\" height=\"503\" title=\"\"><\/p>\n<h3>3. When You Want to Use a Different Package Version<\/h3>\n<p>Now we are working on a project that uses a different version of pakacakge from the one installed on our computer.<\/p>\n<p>Here we can use NPX to use package versions that match the project.<\/p>\n<p>To determine the package version, we just need to add <span style=\"color: #ff6600;\">@ 1.2.3<\/span> after the package name.<span style=\"color: #ff6600;\"> 1.2.3<\/span> is the package version that will be used.<\/p>\n<p>Example:<\/p>\n<p>At the moment <span style=\"color: #ff6600;\">Gulp<\/span> has reached version <span style=\"color: #ff9900;\">4.0.1,<\/span> but I want to use <span style=\"color: #ff9900;\">Gulp<\/span> version <span style=\"color: #ff9900;\">3.9.0.<\/span><\/p>\n<p>Then I have to type:<\/p>\n<pre>npx gulp@3.9.0 --version<\/pre>\n<p>The &#8211;version argument is an argument for checking gulp versions.<\/p>\n<p>Oh yes, <span style=\"color: #ff6600;\">Gulp<\/span> is a build tool. You can read the Gulp tutorial here.<\/p>\n<p>The result:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-243\" src=\"https:\/\/matob.web.id\/random\/wp-content\/uploads\/sites\/2\/2019\/08\/npx-gulp.png\" alt=\"\" width=\"750\" height=\"503\" title=\"\"><\/p>\n<h3>4. When We Don&#8217;t Have Root Access<\/h3>\n<p>Sometimes we will find, the times when we are not given permission to install the Nodejs package globally.<\/p>\n<p>For example, on a server. We are only given permission to use it as a normal user.<\/p>\n<p>Then to install the Nodejs package globally there, we need root access.<\/p>\n<p>But unfortunately we don&#8217;t have that access.<\/p>\n<p>So this is where we can use <span style=\"color: #ff9900;\">npx<\/span>.<\/p>\n<h2>Auto-fallback shell with NPX<\/h2>\n<p>Ever found it like this?<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-244\" src=\"https:\/\/matob.web.id\/random\/wp-content\/uploads\/sites\/2\/2019\/08\/webpack.png\" alt=\"\" width=\"750\" height=\"503\" title=\"\"><\/p>\n<p>We want to run the <span style=\"color: #ff9900;\">webpack<\/span> command, but the command is not installed on the computer.<\/p>\n<p>There I recommend to type the command:<\/p>\n<pre>sudo apt install webpack<\/pre>\n<p>This is a shell-auto fallback.<\/p>\n<p>An auto-fallback shell is a command that will be done when a command is not found.<\/p>\n<p>NPX has a shell-auto fallback feature. How to activate it:<\/p>\n<p>For <span style=\"color: #ff9900;\">shell bash<\/span>:<\/p>\n<pre>source &lt;(npx --shell-auto-fallback bash)<\/pre>\n<p>For <span style=\"color: #ff9900;\">zsh shell<\/span>:<\/p>\n<pre>source &lt;(npx --shell-auto-fallback zsh)<\/pre>\n<p>For <span style=\"color: #ff9900;\">shell fish<\/span>:<\/p>\n<pre>source (npx --shell-auto-fallback fish | psub)<\/pre>\n<p>Let&#8217;s try &#8230;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-245\" src=\"https:\/\/matob.web.id\/random\/wp-content\/uploads\/sites\/2\/2019\/08\/shell-auto-fallback.png\" alt=\"\" width=\"750\" height=\"503\" title=\"\"><\/p>\n<p>First we register NPX as sehll-auto fallback on bash (because I use bash) with the command:<\/p>\n<pre>source &lt;(npx --shell-auto-fallback bash)<\/pre>\n<p>Then I tried to execute the command <span style=\"color: #ff6600;\">gulp@3.9.0<\/span> and <span style=\"color: #ff6600;\">happy-birthday<\/span>.<\/p>\n<p>Oh yeah, when I type in <span style=\"color: #ff6600;\">happy-birthday<\/span> only &#8230;<\/p>\n<p>&#8230; the auto-fallback shell doesn&#8217;t work.<\/p>\n<p>This might be so as not to clash with other linux commands.<\/p>\n<p>Actually, the command:<\/p>\n<pre>npx --shell-auto-fallback bash<\/pre>\n<p>Will generate a bash code for auto-fallback.<\/p>\n<p>We can save this to a <span style=\"color: #ff6600;\">.bashrc<\/span> file for permanent use.<\/p>\n<h2>Running a Server with NPX<\/h2>\n<p>We can also use NPX to run the server. You can try the nodejs package to create servers like nodemon, json-server, and so on.<\/p>\n<p>&nbsp;<\/p>\n<blockquote class=\"twitter-tweet\" data-lang=\"en\">\n<p dir=\"ltr\" lang=\"en\"><a href=\"https:\/\/t.co\/LN4YxfJyuJ\">https:\/\/t.co\/LN4YxfJyuJ<\/a> is really cool &#8212; easily try out APIs!<\/p>\n<p>Also a 1-liner w\/ <a href=\"https:\/\/twitter.com\/hashtag\/npx?src=hash&amp;ref_src=twsrc%5Etfw\" target=\"_blank\" rel=\"noopener\">#npx<\/a>:<\/p>\n<p>`npx json-server <a href=\"https:\/\/t.co\/cQWmVEVjbP\">https:\/\/t.co\/cQWmVEVjbP<\/a>` <a href=\"https:\/\/t.co\/4EAEkt40aP\">pic.twitter.com\/4EAEkt40aP<\/a><\/p>\n<p>\u2014 Kat March\u00e1n (@maybekatz) <a href=\"https:\/\/twitter.com\/maybekatz\/status\/878926190064668672?ref_src=twsrc%5Etfw\" target=\"_blank\" rel=\"noopener\">June 25, 2017<\/a><\/p><\/blockquote>\n<h2>The final word\u2026<\/h2>\n<p>So far we are familiar with the <span style=\"color: #ff9900;\">npx<\/span> function.<\/p>\n<p>NPX is the nodejs package runner to execute the nodejs package. Whereas NPM is the node package manager for managing nodejs packages and projects.<\/p>\n<p>If you like the article we can follow it through <a href=\"https:\/\/matob.web.id\/random\">the Random Creative<\/a> Blog Feed or subscribe to get information from us.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When Matob first saw npx, I thought it was typo from npm. Apparently not\u2026 NPM and NPX are two tools that have different functions. What are the differences? &#8230; and when should we use NPX? Let&#8217;s discuss? NPM vs NPX NPM stands for Node Package Manager. A text based program for Nodejs package management. Whereas [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1852,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-1851","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software"],"_links":{"self":[{"href":"https:\/\/matob.web.id\/news\/wp-json\/wp\/v2\/posts\/1851","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/matob.web.id\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/matob.web.id\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/matob.web.id\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/matob.web.id\/news\/wp-json\/wp\/v2\/comments?post=1851"}],"version-history":[{"count":1,"href":"https:\/\/matob.web.id\/news\/wp-json\/wp\/v2\/posts\/1851\/revisions"}],"predecessor-version":[{"id":1853,"href":"https:\/\/matob.web.id\/news\/wp-json\/wp\/v2\/posts\/1851\/revisions\/1853"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/matob.web.id\/news\/wp-json\/wp\/v2\/media\/1852"}],"wp:attachment":[{"href":"https:\/\/matob.web.id\/news\/wp-json\/wp\/v2\/media?parent=1851"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/matob.web.id\/news\/wp-json\/wp\/v2\/categories?post=1851"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/matob.web.id\/news\/wp-json\/wp\/v2\/tags?post=1851"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}