Node.js in AL2023 - Amazon Linux 2023

Node.js in AL2023

Node.js in AL2023 is represented by versions 18, 20 and 22. All supported Node.js versions are namespaced and can be installed on the same system simultaneously. Namespacing ensures that each Node.js installation is unique within the file system. This is achieved by renaming key directories and files based on the runtime version. The actual executable names will look like node-{MAJOR_VERSION} or npm-{MAJOR_VERSION}. However, only one Node.js version can be active at a time. This active version provides the default directories and file names, such as node, npm or /usr/lib/node_modules, pointing them to the currently active runtime.

This is achieved using the capabilities of the alternatives tool. It is important to remember that the default executable names are virtual and can change at any time when pointing to a different installed Node.js version. This flexibility enables software that uses node in the shebang to select the desired version when invoked. However, when a specific version of Node.js is required, persistence of the version can be achieved by calling the namespaced executables (e.g., node-20 or node-22), which will always use the specified version of the runtime. Additionally, the namespaced executables of the npm tool, such as npm-20 or npm-22, are always associated with the corresponding Node.js version, regardless of the currently active runtime.

Node.js is distributed as several namespaced packages. These packages provide node, a compatible with it version of npm tool, documentation, libraries, and more. The only exception is Node.js 18, which historically is not namespaced and provides node and npm in the nodejs and nodejs-npm packages, respectively. However, the subsequent versions of Node.js are namespaced and begin with "nodejs{MAJOR_VERSION}". For example, node and npm of the Node.js 20 are provided by the nodejs20 and nodejs20-npm packages, respectively.

The alternatives tool provides a single command for switching between Node.js versions. By default, alternatives is configured to be in auto mode, which uses priorities to determine the currently active Node.js version. However, you can activate any installed version at any time. Currently, all supported versions of Node.js have equal priority, meaning the first installed version will be activated automatically.

Some useful examples of using alternatives
  1. Check what alternatives is configured for

    alternatives --list
  2. Check node's current configuration

    alternatives --display node
  3. Interactively change the Node.js version

    alternatives --config node
  4. Switch to manual mode and select a specific version

    alternatives --set node /usr/bin/node-{MAJOR_VERSION}
  5. Switch back to auto version selection mode

    alternatives --auto node