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-platformJavaScript
runtime environment for runningJavaScript
outside of the browser, allowing for server-side functionality, it can be though of as being similar to thePython
interpreternpm
(Node Package Manager
) refers to both the default package manager fornode
and an online registry ofJavaScript
packages- As a software,
npm
is a command-line utility bundled withnode
which managesJavaScript
dependencies in your project - As an online registry it contains over a million open-source
JavaScript
packages
- As a software,
nvm
(Node Version Manager
) is a tool that helps you install and switch between different versions ofnode
/npm
npx
(Node Package Execute
) is packaged withnode
and is a tool to runnpm
packages without installing it globallynpm view create-react-app
will check if the packagecreate-react-app
exists on thenpm
registry, note thatnpm
is used here notnpx
npx create-react-app my-app
will first search locally, theng globally, then fetch thenpm
packagecreate-react-app
from thenpm
registry, 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
node
version for a project is done manually. If the project has an.nvmrc
file,nvm use
will 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 -v
shows the currentnvm
versionnvm list
shows the installednode
versionsnvm
can switch betweennvm current
shows the currently activenode
versionnvm install
installs a given version ofnode
nvm install 24.0.0
installsnode
verson 24.0.0nvm install latest
installs the latest version ofnode
nvm use
sets the currently usednode
versionnvm use 24.0.0
sets currently usednode
to version 24.0.0nvm use latest
sets currently usednode
to the latest version
nvm uninstall
uninstalls a given version ofnode
nvm uninstall 24.0.0
uninstallsnode
verson 24.0.0
Node.js (node)
node -v
shows the current node
version
Node Package Manager (npm)
npm -v
shows the currentnpm
versionnpm init
initialises anode
project in the current directorynpm install
installs all dependencies found inpackage.json
from thenpm
registry tonode_modules
npm install {package_name}
installs a specific module from thenpm
registry tonode_modules
npm list
shows all locally installed packages for the current projectnpm init -y
runsnpm init
with the default optionsnpm config edit
will allow you to edit yournpm init
default 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 thenpm
registry
Node Package Execute (npx)
npx -v
shows the currentnpx
versionnpx {package-name} {other-arguments}
runs a specific package from thenpm
registry 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