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


An ECS instance is created. For more information, see Create an instance by using the wizard.
Note In this topic, the CentOS 7.8 public image is used to create an instance.

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 applicable to 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 to consume existing data.
  • Utilities: a variety of utilities from frontend compression and deployment applications such as grunt to desktop GUI 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.

Step 1: Deploy the Node.js environment

  1. Connect to the ECS instance.
    For more information, see Connection methods.
  2. Deploy the Node.js environment.
    This topic describes two methods used to deploy the Node.js environment. You can use one of the following methods to deploy the Node.js environment based on your business requirements:
    • Method 1: Use NVM to install multiple Node.js versions
      Node Version Manager (NVM) is a software used to manage Node.js versions. You can use NVM to switch among 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 among Node.js versions. To install multiple Node.js versions by using NVM, perform the following operations:
      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.
        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. Install multiple Node.js versions.
        1. Install v6.9.5.
          nvm install v6.9.5
        2. Install v7.4.0.
          nvm install v7.4.0
        Note You can run the nvm list-remote command to view all versions of Node.js.
      5. Check the installed Node.js versions.
        nvm ls
        The following command output indicates that v6.9.5 and v7.4.0 are installed and v7.4.0 is in use:
        ->       v7.4.0
        stable -> 7.4 (-> v7.4.0) (default)
        unstable -> 6.9 (-> v6.9.5) (default)
        Note You can run the nvm use <Version number> command to switch among the Node.js versions. For example, you can run the nvm use v6.9.5 command to switch to Node.js v6.9.5.
    • Method 2: 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, the node and npm files already exist in the bin folder. Therefore, you do not need to recompile the binary file. In this example, Node.js v6.9.5 is installed. Perform the following operations:
      1. Download the Node.js installation package.
      2. Decompress the package.
        tar xvf node-v6.9.5-linux-x64.tar.xz
      3. Run the following commands in sequence to create symbolic links for node and npm.

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

        ln -s /root/node-v6.9.5-linux-x64/bin/node /usr/local/bin/node
        ln -s /root/node-v6.9.5-linux-x64/bin/npm /usr/local/bin/npm
      4. Check the versions of node and npm.
        node -v
        npm -v

        Then, the Node.js environment is installed. By default, the software is installed in the /root/node-v6.9.5-linux-x64/ directory.

        If you want to install the software to another directory such as /opt/node/, run the following commands in sequence.
        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-v6.9.5-linux-x64/* /opt/node/
        3. Remove the symbolic links for node and npm from the source directory.
          rm -f /usr/local/bin/node
          rm -f /usr/local/bin/npm
        4. Create symbolic links for 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. Run the following commands in sequence to create a test project file named example.js.
    1. Go back to the /root directory.
    2. 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 the edit mode and add the following content to the example.js file.
      In this example, port 3000 is occupied by the project. The command output is Hello World. You can configure the project content and a port number 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 the edit mode. Enter :wq and press the Enter key to save and close the file.
  3. Run the project and obtain the port number of the project.
    node ~/example.js &
  4. List the ports on which the system is listening.
    netstat -tpln
    In this example, port 3000 is included in the command output, which indicates that the project is running normally.
  5. Add an inbound rule to the security group of the ECS instance to allow traffic on the specified port.
    In this example, port 3000 is used. For more 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 can access the Internet, 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 3000. The following page is displayed when you access the project.