Mastering Node.js Environments on Alibaba Cloud Linux with NVM and PM2

Prerequisites

  • An Alibaba Cloud ECS instance running Alibaba Cloud Linux.
  • Root or sudo privileges.
  • Basic familiarity with Linux command-line operations.

Install Steps

Install NVM (Node Version Manager)

NVM allows you to install and switch between different Node.js versions seamlessly.

Step 1.1: Install Git and Clone NVM Repository

First, ensure Git is installed. Then, clone the NVM source code from its repository to your local ~/.nvm directory and check out the latest stable version.

sudo yum install -y git
git clone https://github.com/nvm-sh/nvm.git ~/.nvm
cd ~/.nvm
git checkout `git describe --abbrev=0 --tags`

Network Issues in Mainland China? If you experience network connectivity issues with GitHub, you can use a Gitee mirror:

# Ensure git is installed: sudo yum install -y git

git clone https://gitee.com/banjiaojuhao/nvm.git ~/.nvm
cd ~/.nvm
git checkout `git describe --abbrev=0 --tags`

Step 1.2: Activate NVM

To make NVM available in your shell, you need to source it. We'll add it to the system-wide profile so it's available for all users upon login.

echo ". ~/.nvm/nvm.sh" | sudo tee -a /etc/profile
source /etc/profile

After running source /etc/profile, NVM will be available in your current terminal session. For new sessions, it will be loaded automatically. You can verify by typing nvm --version.

Configure NVM Mirrors (Optional, for China Users)

If you encounter issues fetching the list of Node.js versions (e.g., nvm list-remote hangs or fails), configuring NVM to use domestic mirrors in China can significantly improve speed and reliability.

Method 1: Using npmmirror.com

nvm npm_mirror https://npmmirror.com/mirrors/npm/
nvm node_mirror https://npmmirror.com/mirrors/node/

Method 2: Using Tencent Cloud Mirrors

nvm npm_mirror http://mirrors.cloud.tencent.com/npm/
nvm node_mirror http://mirrors.cloud.tencent.com/nodejs-release/

Choose one method. After configuration, try listing remote versions again.

List and Install Node.js Versions

1. List Available Node.js Versions To see all Node.js versions available for installation:

nvm list-remote

This command will output a long list of versions.

2. Install Desired Node.js Versions

You can install any specific version. For example, to install Node.js v6.9.5 and v7.4.0 (though these are quite old, we'll use them as per the example; consider installing LTS versions for new projects, e.g., nvm install --lts):

nvm install v6.9.5
nvm install v7.4.0

You can also install the latest LTS (Long Term Support) version or the latest current version:

nvm install --lts       # Installs the latest LTS version
nvm install node        # Installs the latest current version

3. List Installed Versions

To see the versions you have installed locally:

nvm ls

You should see output similar to this, indicating the installed versions and which one is currently active:


[root@iZXXXXZ .nvm]# nvm ls
         v6.9.5
->       v7.4.0
         system
stable -> 7.4 (-> v7.4.0) (default)
unstable -> 6.9 (-> v6.9.5) (default)

(Note: The stable and unstable aliases in the example above might not be automatically created with newer NVM versions or specific Node versions. The important part is seeing your installed versions and the -> pointing to the active one.)

Switch Between Node.js Versions

To switch to a different installed version of Node.js, use the nvm use command. For example, to switch to v7.4.0:

nvm use v7.4.0

Output:

Now using node v7.4.0 (npm vX.Y.Z)

(The npm version will correspond to the Node.js version selected.)

Set a Default Node.js Version

Sometimes, when you open a new terminal, NVM might not automatically select the last used version, or it might revert to the system's Node.js (if any). To ensure a specific Node.js version is used by default in new shell sessions, set a default alias. For example, to set v18.16.1 (replace with your desired default version) as the default:

nvm install v18.16.1 # Ensure it's installed first
nvm alias default v18.16.1

Now, new terminal sessions will automatically use Node.js v18.16.1.

Install PM2 - The Production Process Manager

PM2 is a powerful, feature-rich process manager for Node.js applications. It keeps your applications alive, provides logging, monitoring, and much more. Install PM2 globally using npm (which was installed with Node.js):

Install PM2

npm install -g pm2

This command uses the currently active Node.js version (and its corresponding npm) to install PM2. Verify the PM2 installation:

pm2 --version

You can now use PM2 to start, stop, monitor, and manage your Node.js applications. For example:

pm2 start app.js --name my-awesome-app
pm2 list
pm2 logs my-awesome-app
pm2 stop my-awesome-app

Conclusion

You have now successfully set up NVM on your Alibaba Cloud Linux instance, enabling you to manage multiple Node.js versions with ease. Additionally, with PM2 installed, you are well-equipped to deploy and manage your Node.js applications robustly in a production-like environment. This setup provides flexibility for development and stability for deployment. Happy coding on Alibaba Cloud!

Community

We're excited to see the community adopt Hyperse-io, raise issues, and provide feedback. Whether it's a feature request, bug report, or a project to showcase, please get involved!