Post

Node.js, npm, nvm and npx Setup

Installing and using node.js, npm, nvm and npx

Node.js, npm, nvm and npx Setup

Node, NPM, NVM and NPX

  • node (Node.js) is a cross-platform JavaScript runtime environment for running JavaScript outside of the browser, allowing for server-side functionality, it can be though of as being similar to the Python interpreter
  • npm (Node Package Manager) refers to both the default package manager for node and an online registry of JavaScript packages
    • As a software, npm is a command-line utility bundled with node which manages JavaScript dependencies in your project
    • As an online registry it contains over a million open-source JavaScript packages
  • nvm (Node Version Manager) is a tool that helps you install and switch between different versions of node / npm
  • npx (Node Package Execute) is packaged with node and is a tool to run npm packages without installing it globally
    • npm view create-react-app will check if the package create-react-app exists on the npm registry, note that npm is used here not npx
    • npx create-react-app my-app will first search locally, theng globally, then fetch the npm package create-react-app from the npm 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 current nvm version
  • nvm list shows the installed node versions nvm can switch between
  • nvm current shows the currently active node version
  • nvm install installs a given version of node
    • nvm install 24.0.0 installs node verson 24.0.0
    • nvm install latest installs the latest version of node
  • nvm use sets the currently used node version
    • nvm use 24.0.0 sets currently used node to version 24.0.0
    • nvm use latest sets currently used node to the latest version
  • nvm uninstall uninstalls a given version of node
    • nvm uninstall 24.0.0 uninstalls node verson 24.0.0

Node.js (node)

node -v shows the current node version

Node Package Manager (npm)

  • npm -v shows the current npm version
  • npm init initialises a node project in the current directory
  • npm install installs all dependencies found in package.json from the npm registry to node_modules
  • npm install {package_name} installs a specific module from the npm registry to node_modules
  • npm list shows all locally installed packages for the current project
  • npm init -y runs npm init with the default options
  • npm config edit will allow you to edit your npm init default options, or you can edit manually as below
    • npm 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 the npm registry

Node Package Execute (npx)

  • npx -v shows the current npx version
  • npx {package-name} {other-arguments} runs a specific package from the npm 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
This post is licensed under CC BY 4.0 by the author.