All Products
Search
Document Center

Elastic Compute Service:Manually deploy a Node.js environment

Last Updated:Aug 20, 2025

This topic describes how to manually deploy a Node.js environment on an Elastic Compute Service (ECS) instance.

Prerequisites

Important

Alibaba Cloud Linux 2 and CentOS 7.x support only Node.js 17.x and earlier versions.

An ECS instance is created and meets the following requirements:

  • The instance is assigned a public IP address by the system or is associated with an elastic IP address (EIP). For more information, see Enable public bandwidth.

  • The operating system is Alibaba Cloud Linux 3, Alibaba Cloud Linux 2, CentOS 7.x, Ubuntu 18.x or later, Debian 10.x or later, or Windows.

  • In the instance's security group, add inbound rules to allow connections on port 22 (for SSH) and port 3389 (for RDP). For enhanced security, we recommend allowing access only from specific IP addresses or ranges, rather than from all IPv4 addresses (0.0.0.0/0). For information about how to add a security group rule, see Add a security group rule.

Linux

This guide uses Node Version Manager (NVM) to deploy Node.js on Linux. NVM offers several advantages over other methods:

  • It is not limited by system repository versions, which ensures you can install the latest Node.js release.

  • It automatically configures environment variables, unlike installations from precompiled binaries.

  • It significantly reduces installation time compared to compiling from source code and requires no specialized skills.

Furthermore, NVM supports managing multiple Node.js versions, installs them in your home directory without requiring sudo permissions, and helps reduce security risks.

Install Node.js

  1. Connect to the instance on which you want to install Node.js. For more information, see Use Workbench to log on to a Linux instance over SSH.

  2. Install the distributed version management system Git.

    • Alibaba Cloud Linux 2, Alibaba Cloud Linux 3, or CentOS 7.x

      sudo yum install git -y
    • Ubuntu 18.x and later or Debian 10.x and later

      sudo apt update
      sudo apt install git -y
  3. Use Git to clone the NVM source code into the ~/.nvm directory and check out the latest stable release.

    Note

    The source code of NVM may fail to be cloned due to network issues. If a failure occurs, we recommend that you re-clone the source code.

    git clone https://gitee.com/mirrors/nvm.git ~/.nvm && cd ~/.nvm && git checkout `git describe --abbrev=0 --tags`
  4. Run the following commands in sequence to configure the environment variables of NVM:

    sudo sh -c 'echo ". ~/.nvm/nvm.sh" >> /etc/profile'
    source /etc/profile
  5. To speed up downloads, run the following command to set the Node.js mirror:

    export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node
  6. Run the following command to check the version of Node.js:

    nvm list-remote
  7. Install multiple Node.js versions.

    Important

    Alibaba Cloud Linux 2 and CentOS 7.x only support Node.js 17.x and earlier. If you are using one of these operating systems, replace the version in the following command with a version like v17.9.1.

    1. Install Node.js 23.3.0.

      nvm install v23.3.0
    2. Install Node.js 22.11.0.

      nvm install v22.11.0
  8. Run the following command to check the installed Node.js versions:

    nvm ls

    If the following sample command output is returned, v22.11.0 and v23.3.0 are installed and v22.11.0 is being used.

    image

    Note

    You can run the nvm use <Version number> command to switch between the versions of Node.js. For example, to switch to Node.js 23.3.0, run the nvm use v23.3.0 command.

Deploy a test project

  1. Create a test project file named example.js.

    1. Run the following command to go back to the home directory:

      cd
    2. Run the following command to create the example.js test project file:

      touch example.js
  2. Modify the example.js project file.

    1. Run the following command to open the example.js file:

      vim example.js
    2. Press the I key to enter Insert mode and add the following content to the example.js file.

      In this example, port 3000 is occupied by the project, and the command output is Hello World. You can configure the project content (res.end) and a port number (const port) based on your business requirements.

      const http = require('http');
      const hostname = '0.0.0.0';
      const port = 3000;
      const server = http.createServer((req, res) => { 
          res.statusCode = 200;
          res.setHeader('Content-Type', 'text/plain');
          res.end('Hello World\n');
      }); 
      
      server.listen(port, hostname, () => { 
          console.log(`Server running at http://${hostname}:${port}/`);
      });
    3. After you add the preceding content, press the Esc key to exit Insert mode. Enter :wq and press the Enter key to save and close the file.

  3. Run the project in the background:

    node ~/example.js &
  4. Run the following command to list the ports on which the system is listening:

    netstat -tpln

    In this example, port 3000 is displayed in the command output, which indicates that the project is running as expected.

  5. Add an inbound rule to a security group of the ECS instance to open the specified port.

    In this example, port 3000 is used. For information about how to add rules to a security group, see Add a security group rule.

    image

  6. From your local computer or any computer with internet access, open a browser and enter http://<Public IP address of the instance>:<Port number> in the address bar.

    In this example, <Port number> is set to 3000. The following page is displayed when you access the project.

Windows

To deploy Node.js on Windows, download the installer from the official website.

Install Node.js

  1. Connect to the instance on which you want to install Node.js. For more information, see Use Workbench to connect to a Windows instance over RDP.

  2. Visit the official website of Node.js and click Download in the top navigation bar.

    image

  3. Download the installation package.

    The download page provides a variety of installation methods. Each method is suitable for different needs and preferences. In this example, Prebuilt Installer is selected.

    Note

    We recommend selecting a Long-Term Support (LTS) version for better stability.

    Installation methods

    Description

    Package Manager

    Use the package manager on the operating system such as npm, apt, yum, or brew to install Node.js.

    Prebuilt Installer

    Use a pre-compiled installation package for your operating system to install Node.js, such as a .msi file for Windows or a .pkg file for macOS. Such a package contains the Node.js runtime and npm, and usually contains some necessary dependencies.

    Prebuilt Binaries

    Use a Node.js executable file compiled for your platform to install Node.js. No installation wizard is provided. You must manually decompress the package and configure environment variables.

    Source Code

    Use the source code to install Node.js. After you decompress the package, you must use specific commands or scripts to configure and compile the source code to install Node.js.

  4. Double-click the downloaded installation package and follow the installation wizard to install Node.js.

    image

  5. Open the Command Prompt or PowerShell.

    image

  6. In the Command Prompt window, enter node -v and npm -v and press the Enter key. If the output is similar to that in the following figure, Node.js is installed.

    image

Deploy a test project

  1. Create a test project folder named newfolder and an example.txt file.

    image

  2. Double-click the example.txt file and copy and paste the following content to the file.

    In this example, port 3000 is occupied by the project, and the command output is Hello World. You can configure the project content (res.end) and a port number (const port) based on your business requirements.

    const http = require('http');
    const hostname = '0.0.0.0';
    const port = 3000;
    const server = http.createServer((req, res) => {
        // Set the status code to 200.
        res.statusCode = 200;
        // Set the content type to plaintext.
        res.setHeader('Content-Type', 'text/plain');
        // Add the Cache-Control header to disable caching.
        res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0');
        // Add the Pragma header to disable caching to be compatible with the old browser.
        res.setHeader('Pragma', 'no-cache');
        // Set the Expires header to a past date, which makes the content immediately expire. Alternatively, specify a specific expiration date, such as 'Expires: Tue, 03 Jul 2001 06:00:00 GMT'.
        res.setHeader('Expires', '0'); 
        // Send the response body.
        res.end('Hello World\n');
    });
    // Listen on the specified port and hostname.
    server.listen(port, hostname, () => {
        console.log(`Server running at http://${hostname}:${port}/`);
    });
  3. Rename the example.txt file to example.js. image

  4. Go to the project directory and run the npm init command to initialize the project. Enter the project information as prompted to generate a package.json file.image

  5. Run the Node.js project by running the node example.js command. After you successfully deploy the Node.js environment, the Node.js server runs and displays relevant information. image

  6. Add an inbound security group rule to the security group of the ECS instance to open port 3000 configured in the project. For more information, see Add a security group rule.image

  7. From your local computer or any computer with internet access, open a browser and enter http://<Public IP address of the ECS instance>:<Project port number>.

    In this example, <Port number> is set to 3000. The following page is displayed when you access the project.image