This topic describes the procedure and precautions for manually deploying a Node.js environment on an Elastic Compute Service (ECS) instance to help developers build a Node.js development or runtime environment quickly and smoothly.
Prerequisites
Alibaba Cloud Linux 2 and CentOS 7.x support the deployment of 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 EIP.
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.
The inbound rules are added to the security group to which the instance belongs to open port 22 (SSH) and port 3389 (RDP) to support remote logon and remote desktop connection. To enhance security, we recommend that you allow only the CIDR blocks that you want to access and do not grant access permissions to 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
Deploy Node.js on Linux. In this example, Node Version Manager (NVM) is used. Compared with package manager installation, NVM is not limited by the repository version to ensure that the latest Node.js version is obtained. Compared with the method of downloading precompiled binary packages, NVM does not require environment variable settings. Compared with the method of compiling and installing from source code, NVM greatly shortens the installation time and does not require user compilation skills. In addition, NVM supports multi-version management to facilitate version switching, and Node.js that you install is located in the home directory without sudo permissions, effectively reducing security risks.
Install Node.js
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.
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 and Debian 10.x and later
sudo apt update sudo apt install git -y
Use Git to clone the source code of NVM to the local ~/.nvm directory and check for the latest update.
NoteThe source code of NVM may fail to be cloned due to network issues. If a failure occurs, we recommend that you reclone the source code.
git clone https://gitee.com/mirrors/nvm.git ~/.nvm && cd ~/.nvm && git checkout `git describe --abbrev=0 --tags`
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
Run the following command to set the npm image repository to the Alibaba Cloud image repository to speed up the download of Node.js:
export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node
Run the following command to check the version of Node.js:
nvm list-remote
Install multiple Node.js versions.
ImportantAlibaba Cloud Linux 2 and CentOS 7.x only support Node.js 7.x and later. Replace the version in the following command with Node.js 17.x or later. For example, to install Node.js 17.9.1, run the nvm install v17.9.1 command.
Install Node.js 23.3.0.
nvm install v23.3.0
Install Node.js 22.11.0.
nvm install v22.11.0
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.
NoteYou 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
Create a test project file named
example.js
.Run the following command to go back to the home directory:
cd
Run the following command to create the example.js test project file:
touch example.js
Modify the
example.js
project file.Run the following command to open the
example.js
file:vim example.js
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}/`); });
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.
Run the following command to run the project and obtain the port number of the project:
node ~/example.js &
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.
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.
On your Windows computer or a Windows computer that has access to 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 set to 3000. The following page is displayed when you access the project.
Windows
On Windows, you can download the corresponding installation package for Node.js deployment from the official website.
Install Node.js
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.
Visit the official website of Node.js and click Download in the top navigation bar.
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.
NoteSelect a version labeled with long-term support (LTS), which indicates that the version has been tested for a long period of time and is relatively stable.
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.
Double-click the downloaded installation package and follow the installation wizard to install Node.js.
Open the Command Prompt or PowerShell.
In the Command Prompt window, enter
node -v
andnpm -v
and press the Enter key. If the output is similar to that in the following figure, Node.js is installed.
Deploy a test project
Create a test project folder named newfolder and an example.txt file.
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) => { 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}/`); });
Change the suffix of the example.txt file to example.js.
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.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.
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.
On a Windows host or a Windows host that can access the Internet, 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.