This article may contain affiliate links. If you buy some products using those links, I may receive monetary benefits. See affiliate disclosure here
When it comes to the JavaScript ecosystem, the difference between Node, NPM, and NVM is one thing that often confuses people. If you are already familiar with it, then it might be obvious for you. Otherwise, here it goes.
Let’s start with Node, or Node.js.
First of all, Node.js is not another programming language like JavaScript, or even a JavaScript framework. Node.js is just the runtime environment that allows running JavaScript code on whichever machine you install it, including servers, desktops, embedded systems, so on.
It’s written in C++, and uses Chrome’s V8 engine at its core, with some modification. Node.js also comes with several core modules like the HTTP module, which can be used to create a webserver, filesystem module, which can interact with the files and folders on your system, etc.
So when someone says “this application is built using Node.js”, the real meaning is that it is built using JavaScript and run in an environment provided by Node.
By the way, web browsers like Google Chrome also uses the V8 engine to execute JavaScript. Node.js just brought JavaScript to the server-side.
That also means you can run JavaScript files just like you run a PHP or Python Script. For example, if you have file called main.js in the filesystem, you can run it from the terminal using the node command followed by the path to the file. And you can view the output in the terminal itself.
Now what is NPM?
NPM is Node Package Manager. It is the default repository from where you can install packages or dependencies for your JavaScript projects. It comes bundled with Node.js and is also said to be the largest software repository.
The npm cli is also often installed when you install node.js. So you can use npm-based commands to install, update, or remove packages from the command line.
So why NPM is “Node Package Manager” instead of just “JavaScript Package Manager”?
Although you can use these packages on the frontend, solely in the browser, without installing Node.js, NPM’s primary focus is on Node.js projects.
Moreover, even if you are using a package on the frontend, you might still need to bundle the various modules together, and make it ready for browsers. Because older browsers may not support the latest ES6 module syntax, like the “import” statement used to load functions from another module. Tools like Webpack can bundle the various modules and make them ready for browsers. And it’s this bundled file that you add to the page HTML.
What more? Even to add a package to your JavaScript project, you need to run the npm install command. And this also requires Node environment.
However, if you are determined to use a package directly without using Node.js, it is possible. Just go to the package page on npmjs, get the cdn link, and include it in your html within script tag.
Now what’s the deal with NVM?
NVM stands for Node Version Manager. So it’s a tool that allows you to switch between different versions of Node.js without headache.
Or in other words, NVM allows you to manage multiple node.js versions on the same system.
What is its use?
For instance, one project may require Node.js 18 while another one may require Node.js 16, due to legacy codes or for compatibility reasons. In such cases, NVM allows you to install 16, 18, or any other version and switch between the them from the terminal. Run the nvm use command followed by the version you want to switch to.
Typically, you go to the Nodejs website, download the latest installer file and install it on your system. Whereas NVM gives an alternative way to install Node.js, so that you don’t need to download node.js manually.
NVM is supported on all major operating systems. And you can find the details on the official github page.
What’s NPX?
NPX also comes bundled with NPM. But in contrast to the NPM command which only installs a package, the NPX command executes a package.
You don’t even need to install the package prior to executing it. NPX downloads the package temporarily and executes the binary, which is usually located inside the /bin/
folder.
Let’s consider an example. You want to start an HTTP server without installing the package. For that you can just run the following command in the terminal:
npx http-server
And you’ll have a localhost server running on your machine.
If you are wondering where the executable came from, you can find it within the /bin/ directory of the package repository.
With that being said, there is an difference between running npx without installing the package and after installing the package.
If it is installed using the ‘npm install’ command, the installed version no. will be added to the package.json file under dependencies.
Then if you run npx command, then it will run the installed version from the local filesystem – npx will not fetch the latest version from the remote npm repository.
And if you still want to fetch the latest version, you can add the ‘–ignore-existing’ flag, which forces npx to fetch the latest version from the remote repo.
I hope that helps to clear the confusions around these terms.