This script is tailored for sysadmins and server environments where managing Node.js versions at the system level is crucial. Unlike user-based NVM installations that are common for development environments, system-based NVM ensures consistent Node.js versions across all users and applications on the server.
-
Install Node.js Versions: Download and install specific Node.js versions from the official Node.js release repository.
-
List Installed Versions: Display a list of Node.js versions installed on your system, indicating the currently active version.
-
Set Active Version: Set the active Node.js version, creating symbolic links for node, npx, and npm.
-
Remove Installed Version: Safely remove an installed Node.js version, excluding the currently active version.
-
Patch Updates: Check for and install updates to existing Node.js versions, with an option to clean up old versions.
sudo apt purge --auto-remove nodejs
# in my case I will use opt
cd /opt
sudo git clone https://github.com/Watchdog0x/nvm-light.git && cd nvm-light
sudo ln -rfs nvml_completion /etc/bash_completion.d/
Note
The nvml_completion script provides command-line completion for nvml commands, helping you discover available versions and options. After linking, you can use tab completion to explore the available Node.js versions and subcommands.
sudo ln -rfs nvml /usr/local/bin/
source ~/.bashrc
sudo nvml -i 20.9.0 -s 20.9.0 -l
Output
Node.js version 20.9.0 downloaded successfully to /opt/nvm-light/nodejs
Node.js version 20.9.0 extracted successfully
Node.js version 20.9.0 has been set successfully.
Available Node.js versions installed on your system:
* Node.js 20.9.0 (Running)
Note
Use -i
to install, -s
to set the Node.js version, and -l
to list them for checking.
Important
The global installation path for npm packages (npm install -g) is controlled by the system administrator. The standard prefix is set to /usr/local. Ensure that users have the necessary permissions to install global packages or consider running npm commands with elevated privileges.
nvml [OPTIONS]
-i, --install
: Install a specific Node.js version.-l, --list
: List available Node.js versions on your system.-s, --set
: Set the active Node.js version.-r, --remove
: Remove an installed Node.js version.-p, --patch
: Update all installed Node.js versions to the latest. Subcommandclean
removes old versions.-v, --version
: Print the version of nvml-h, --help
: Display the help message.
sudo crontab -e
# Example: Run nvml patch (update) daily at midnight and log the output
**0 0 * * * /path/to/nvm-script/nvml -p >> /var/log/nvml-update.log 2>&1
Note
When using sudo crontab -e
to edit the crontab for the root user, you can also add the clean
option to enable automatic cleanup. Ensure you have the necessary permissions and use this command responsibly.
Contributions are welcome! Feel free to open issues, submit pull requests, or provide suggestions. Please follow the Contributing Guidelines.
This project is licensed under the MIT License