Node.js, npm, nvm and npx Setup
Installing and using node.js, npm, nvm and npx
Node, NPM, NVM and NPX
node(Node.js) is a cross-platformJavaScriptruntime environment for runningJavaScriptoutside of the browser, allowing for server-side functionality, it can be though of as being similar to thePythoninterpreternpm(Node Package Manager) refers to both the default package manager fornodeand an online registry ofJavaScriptpackages- As a software,
npmis a command-line utility bundled withnodewhich managesJavaScriptdependencies in your project - As an online registry it contains over a million open-source
JavaScriptpackages
- As a software,
nvm(Node Version Manager) is a tool that helps you install and switch between different versions ofnode/npmnpx(Node Package Execute) is packaged withnodeand is a tool to runnpmpackages without installing it globallynpm view create-react-appwill check if the packagecreate-react-appexists on thenpmregistry, note thatnpmis used here notnpxnpx create-react-app my-appwill first search locally, theng globally, then fetch thenpmpackagecreate-react-appfrom thenpmregistry, and run it
General Information
When a project specifies a node version, this is similar to specifying a version for a Python project, where it assumed the modules were written to be compatible with that version.
The node version can be switched using nvm, which also changes the corresponding version of npm, and you can check the current version via node -v.
Many packages aren’t node version specific, but as JavaScript has evolved (ES6, ES7…) new features have been added and some packages may require a newer a node version to use these new language features
[!NOTE] Switching
nodeversion for a project is done manually. If the project has an.nvmrcfile,nvm usewill follow it, otherwise specify a version eg.nvm use x.y.z
Installation
The first thing to install on a new machine is Node Version Manager (nvm). nvm was built for Unix-based systems such as maxOS / Linux but there is an unofficial Windows version that can be found on GitHub here. Once nvm is installed on your machine you should be able to install node / npm / npx using the commands below
Commands
Below are some useful example commands for nvm, node, npm and npx
Node Version Manager (nvm)
nvm -vshows the currentnvmversionnvm listshows the installednodeversionsnvmcan switch betweennvm currentshows the currently activenodeversionnvm installinstalls a given version ofnodenvm install 24.0.0installsnodeverson 24.0.0nvm install latestinstalls the latest version ofnode
nvm usesets the currently usednodeversionnvm use 24.0.0sets currently usednodeto version 24.0.0nvm use latestsets currently usednodeto the latest version
nvm uninstalluninstalls a given version ofnodenvm uninstall 24.0.0uninstallsnodeverson 24.0.0
Node.js (node)
node -v shows the current node version
Node Package Manager (npm)
npm -vshows the currentnpmversionnpm initinitialises anodeproject in the current directorynpm installinstalls all dependencies found inpackage.jsonfrom thenpmregistry tonode_modulesnpm install {package_name}installs a specific module from thenpmregistry tonode_modulesnpm listshows all locally installed packages for the current projectnpm init -yrunsnpm initwith the default optionsnpm config editwill allow you to edit yournpm initdefault options, or you can edit manually as belownpm set init.author.name "Your name"npm set init.author.email "your@email.com"npm set init.author.url "https://your-url.com"npm set init.license "MIT"npm set init.version "1.0.0"
npm view {package-name}shows details about a specific package on thenpmregistry
Node Package Execute (npx)
npx -vshows the currentnpxversionnpx {package-name} {other-arguments}runs a specific package from thenpmregistry without installing it
Miscellaneous
Example Project Setup
To initialise a node project in the current directory with some necessary files for GitHub repository, use some variant of the commands below
1
2
3
npx license mit > LICENSE
npx gitignore node
npm init -y