All Products
Document Center

Elastic Compute Service:Deploy a Node.js environment on a CentOS 7 instance

Last Updated:Oct 26, 2023

Node.js is a JavaScript runtime environment that runs on the Chrome V8 JavaScript engine. You can use Node.js to build scalable network applications. This topic describes how to install Node.js and deploy a test project on an Elastic Compute Service (ECS) instance that runs CentOS 7.x.

Background Information

Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js is ideal for data-intensive real-time applications that run on distributed devices. The Node.js package manager (npm) is an ecosystem of open-source libraries. Node.js is suitable for the following scenarios:

  • Real-time applications: instant messaging and real-time push notification applications, such as Socket.IO.

  • Distributed applications: applications that perform efficient parallel I/O that consumes existing data.

  • Utilities: a variety of utilities from frontend compression and deployment applications such as grunt to graphical desktop applications.

  • Game applications: real-time and high-concurrency applications in the gaming field, such as the Pomelo framework of NetEase.

  • Web rendering applications: applications that use stable interfaces to improve the rendering performance of web pages.

  • Consistent frontend and backend programming environments: applications that allow frontend developers to take on server-side development, such as the full-stack JavaScript MongoDB, Express.js, AngularJS, and Node.js. (MEAN) framework.


ECS instances are created and meet the following requirements:

  • The instances are associated with auto-assigned public IP addresses or elastic IP addresses (EIPs).

  • The instances run CentOS 7.x.

  • Inbound rules are added to security groups of the instances to open port 22. For information about how to add a security group rule, see Add a security group rule.


Quick deployment

You can use Resource Orchestration Service (ROS) and Node Version Manager (NVM) to quickly install Node.js v14.0.0 and Node.js v16.0.0 and deploy a test project named example.js.

  1. Go to the Create Stack page in the ROS console.

  2. In the upper-left corner of the top navigation bar, select a region. 地域

  3. In the Configure Template Parameters step, enter a name for the stack and select or create an ECS instance.

    If you choose to create an ECS instance, configure the Zone ID, Instance Type, System Disk Category, and Logon Password parameters.


    After you configure the preceding parameters, a pay-as-you-go ECS instance that runs CentOS 7 is created. The instance is assigned a public IP address and uses the pay-by-traffic billing method for network usage with a peak bandwidth of 1 Mbit/s. For more information about the billing of ECS and public bandwidth, see Billing overview.

  4. Confirm that the specified information is correct and click Create.

    On the Stack Information tab of the stack details page, check the status of the stack to determine whether the Node.js environment is deployed on the created or selected ECS instance. When the Node.js environment is deployed, the value of Status changes from Creating to Created.

  5. Click the Outputs tab and then click the link in the Value column corresponding to ExampleUrl to access the example.js project.

    The following page is displayed when you access the project.


Subsequently, when you use Node.js, you can run the nvm install <Version number> command to install a specific version of Node.js and run the nvm use <Version number> command to switch to a specific version of Node.js. For example, to switch to Node.js v18.0.0, run the nvm use v18.0.0 command.

Manual deployment

Step 1: Deploy a Node.js environment

  1. Connect to a created ECS instance.

  2. Deploy a Node.js environment.

    Use NVM to install multiple Node.js versions

    NVM is software that is used to manage Node.js versions. You can use NVM to switch between Node.js versions with ease. NVM is suitable for developers who are dedicated to Node.js or users who want to efficiently update or switch between Node.js versions. To install multiple Node.js versions by using NVM, perform the following steps:

    1. Install the distributed version management system Git.

      yum install git -y
    2. Use Git to clone the source code of NVM to the local ~/.nvm directory and check for the latest update.


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

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

      echo ". ~/.nvm/" >> /etc/profile
      source /etc/profile
    4. Run the following command to set the npm image repository to the Alibaba Cloud image repository to speed up the download of Node.js:

    5. Run the following command to check the version of Node.js:

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

      1. Run the following command to install Node.js v14.0.0:

        nvm install v14.0.0
      2. Run the following command to install Node.js v16.0.0:

        nvm install v16.0.0
    7. Run the following command to check the installed Node.js versions:

      nvm ls

      The following command output indicates that v14.0.0 and v16.0.0 are installed and v16.0.0 is in use:

      ->      v16.0.0
      default -> v14.0.0
      iojs -> N/A (default)
      unstable -> N/A (default)
      node -> stable (-> v16.0.0) (default)
      stable -> 16.0 (-> v16.0.0) (default)
      lts/* -> lts/hydrogen (-> N/A)
      lts/argon -> v4.9.1 (-> N/A)
      lts/boron -> v6.17.1 (-> N/A)
      lts/carbon -> v8.17.0 (-> N/A)
      lts/dubnium -> v10.24.1 (-> N/A)
      lts/erbium -> v12.22.12 (-> N/A)
      lts/fermium -> v14.21.3 (-> N/A)
      lts/gallium -> v16.20.1 (-> N/A)
      lts/hydrogen -> v18.16.1 (-> N/A)

      You can run the nvm use <Version number> command to switch between the Node.js versions. For example, to switch to Node.js v18.0.0, run the nvm use v18.0.0 command.

    Use binary files to install a Node.js version

    The installation package used in this method is a compiled binary file. After you decompress the package, you can find node and npm in the bin folder. You do not need to recompile the files. In this example, Node.js v16.0.0 is installed. Perform the following operations:

    1. Run the following command to download the Node.js installation package:


      To download the installation packages for other Node.js versions, visit the Node.js official website.

    2. Run the following command to decompress the Node.js installation package:

      tar xvf node-v16.0.0-linux-x64.tar.xz
    3. Run the following commands in sequence to create symbolic links to node and npm.

      After you create the symbolic links, you can directly run the node and npm commands in an arbitrary directory.

      ln -s /root/node-v16.0.0-linux-x64/bin/node /usr/local/bin/node
      ln -s /root/node-v16.0.0-linux-x64/bin/npm /usr/local/bin/npm
    4. Run the following commands in sequence to check the versions of node and npm:

      node -v
      npm -v

      Until now, the Node.js environment is installed. By default, the software is installed in the /root/node-v16.0.0-linux-x64/ directory.

      If you want to install the software to another directory such as /opt/node/, perform the following steps:

      1. Create the /opt/node/ directory.

        mkdir -p /opt/node/
      2. Move all files of Node.js to the /opt/node/ directory.

        mv /root/node-v16.0.0-linux-x64/* /opt/node/
      3. Remove the symbolic links to node and npm from the original directory.

        rm -f /usr/local/bin/node
        rm -f /usr/local/bin/npm
      4. Create symbolic links to node and npm in the /opt/node/ directory.

        ln -s /opt/node/bin/node /usr/local/bin/node
        ln -s /opt/node/bin/npm /usr/local/bin/npm

Step 2: Deploy a test project

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

    1. Run the following command to go back to the /root directory:

    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 = '';
      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 following command to run the project and obtain the port number of the project:

    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.

  6. On your Windows computer or a Windows computer that has Internet connectivity, open a browser and enter http://<Public IP address of the ECS 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.