LinuxCommandLibrary

nodenv

Manage multiple Node.js versions

TLDR

Install a specific version of Node.js

$ nodenv install [version]
copy

Display a list of available versions
$ nodenv install --list
copy

Use a specific version of Node.js across the whole system
$ nodenv global [version]
copy

Use a specific version of Node.js with a directory
$ nodenv local [version]
copy

Display the Node.js version for the current directory
$ nodenv version
copy

Display the location of a Node.js installed command (e.g. npm)
$ nodenv which [command]
copy

SYNOPSIS

nodenv <command> [<arguments>...]
nodenv install <version>
nodenv global <version>
nodenv local <version>
nodenv versions
nodenv which <executable>
nodenv rehash

PARAMETERS

install <version>
    Installs a specified Node.js version. This command typically requires the node-build plugin to function.

global <version>
    Sets the default Node.js version for all shells. This version is used when no local or shell-specific version is set.

local <version>
    Sets the Node.js version for the current directory and its subdirectories by creating a .node-version file.

versions
    Lists all installed Node.js versions, indicating which one is currently active.

which <executable>
    Displays the full path to the executable (e.g., node, npm) that nodenv will run for the current context.

rehash
    Installs shims for all Node.js executables and globally installed npm packages known to nodenv. Essential to run after installing new versions or global packages.

shell <version>
    Sets the Node.js version for the current shell session only. This setting overrides global and local versions and is temporary.

uninstall <version>
    Uninstalls a specific Node.js version from your system.

DESCRIPTION

nodenv is a lightweight command-line utility designed for managing multiple Node.js versions on a single system. It empowers developers to seamlessly switch between different Node.js environments, whether globally for all projects or locally on a per-project basis, utilizing `.node-version` files.

The core mechanism of nodenv involves inserting its "shims" directory at the front of your PATH environment variable. This setup allows nodenv to intercept calls to Node.js related executables like node, npm, and npx. When a Node.js command is invoked, nodenv intelligently determines the appropriate Node.js version to use based on the current directory, the presence of `.node-version` files, and global configurations, before transparently passing the command to the selected Node.js installation.

This approach emphasizes simplicity and non-invasiveness, requiring minimal shell integration. It's often extended with plugins like node-build to streamline the installation of new Node.js versions. By ensuring project-specific Node.js environments without conflicts, nodenv is an invaluable tool for development workflows that require support for various Node.js versions.

CAVEATS

nodenv relies heavily on manipulating your PATH environment variable; incorrect shell setup can lead to issues. Users must explicitly run nodenv rehash after installing new Node.js versions or global npm packages for new executables to be discoverable via shims. It does not ship with an installer for Node.js itself, relying on plugins like node-build or manual installations.

PLUGINS

nodenv's core functionality can be significantly extended through a robust plugin system. The most crucial plugin is node-build, which provides the install command, enabling effortless downloading and compiling of various Node.js versions directly.

SHIMS

A fundamental concept in nodenv is the use of "shims." These are small executable scripts placed in your PATH that intercept calls to Node.js commands (like node, npm, npx). When a shim is executed, nodenv determines the correct Node.js version for the current context and then transparently passes the command to the actual Node.js executable.

HISTORY

nodenv was inspired by the design principles of rbenv, a popular Ruby version manager. It emerged as an alternative to more intrusive version management solutions, emphasizing simplicity, minimal shell integration, and an extensible plugin-based architecture. Its development focuses on being unopinionated and providing a clean way to manage Node.js versions through PATH manipulation and shims.

SEE ALSO

rbenv(1), pyenv(1), nvm(1), PATH(7)

Copied to clipboard