Deploy a Node.js environment
This guide shows you how to manually deploy a Node.js environment on an Alibaba Cloud ECS instance and helps you quickly set it up for development or runtime.
Prerequisites
Alibaba Cloud Linux 2 and CentOS 7.x support only Node.js 17.x and earlier versions. Later versions are not supported.
Before you begin, ensure that your ECS instance meets the following requirements:
The instance is assigned a static public IP address or an Elastic IP Address (EIP). For more information, see Assign a public IP address.
The operating system is one of the following: Alibaba Cloud Linux 3/2, CentOS 7.x, Ubuntu 18.x or later, Debian 10.x or later, or Windows.
In the instance's security group, add an inbound rule to allow traffic on port 22 (SSH) and port 3389 (RDP). For enhanced security, we recommend allowing traffic only from specific IP address ranges instead of from all IPv4 addresses (0.0.0.0/0). For details, see Add a security group rule.
Linux systems
This guide uses Node Version Manager (NVM) to deploy Node.js on Linux. NVM offers several advantages over other methods: it lets you install the latest Node.js versions, automatically handles environment variables, and avoids the long compilation times of building from source code. NVM also allows you to manage and switch between multiple Node.js versions. Because NVM installs Node.js in your home directory, it does not require sudo permissions, thus enhancing security.
Install Node.js
Connect to your instance. For details, see Connect to a Linux instance by using Workbench.
Install Git.
Alibaba Cloud Linux 3/2, CentOS 7.x
sudo yum install git -yUbuntu 18.x or later & Debian 10.x or later
sudo apt update sudo apt install git -y
Clone the NVM source code to the ~/.nvm directory and check out the latest release.
NoteCloning may fail due to network issues. If this occurs, try running the command again.
git clone https://gitee.com/mirrors/nvm.git ~/.nvm && cd ~/.nvm && git checkout `git describe --abbrev=0 --tags`[root@iZudy4ge0p0zdtZ ~]# git clone https://gitee.com/mirrors/nvm.git ~/.nvm && cd ~/.nvm && git checkout `git describe --abbrev=0 --tags` Cloning into '/root/.nvm'... remote: Enumerating objects: 9366, done. remote: Counting objects: 100% (802/802), done. remote: Compressing objects: 100% (47/47), done. remote: Total 9366 (delta 765), reused 790 (delta 754), pack-reused 8564 Receiving objects: 100% (9366/9366), 2.86 MiB | 784.00 KiB/s, done. Resolving deltas: 100% (6121/6121), done. Note: switching to 'v0.39.7'. You are in a 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by switching back to a branch. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -c with the switch command. Example: git switch -c <new-branch-name> Or undo this operation with: git switch - Turn off this advice by setting config variable advice.detachedHead to false HEAD is now at bab86d5 v0.39.7Configure the NVM environment variables:
sudo sh -c 'echo ". ~/.nvm/nvm.sh" >> /etc/profile' source /etc/profileTo speed up Node.js downloads, set the npm mirror to an Alibaba Cloud mirror:
export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/nodeRun the following command to list the available remote Node.js versions.
nvm list-remoteInstall multiple Node.js versions.
ImportantAlibaba Cloud Linux 2 and CentOS 7.x support only Node.js 17.x and earlier. On these systems, modify the following commands to install a supported version, such as v17.9.1, by running
nvm install v17.9.1.Install version v23.3.0.
nvm install v23.3.0Install version v22.11.0.
nvm install v22.11.0
List the installed Node.js versions.
nvm lsThe following output indicates that versions v22.11.0 and v23.3.0 are installed, and v22.11.0 is the currently active version.
-> v22.11.0 v23.3.0 default -> v23.3.0 iojs -> N/A (default) unstable -> N/A (default) node -> stable (-> v23.3.0) (default) stable -> 23.3 (-> v23.3.0) (default) lts/* -> lts/jod (-> v22.11.0) 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.2 (-> N/A) lts/hydrogen -> v18.20.5 (-> N/A) lts/iron -> v20.18.1 (-> N/A) lts/jod -> v22.11.0NoteYou can use the
nvm use <version>command to switch between Node.js versions. For example, to switch to Node.js v23.3.0, runnvm use v23.3.0.
Deploy a test project
Create a test project file named
example.js.Go to your home directory.
cdCreate the example.js file.
touch example.js
Edit the
example.jsfile.Run the following command to open
example.js.vim example.jsPress i to enter Insert mode, and then add the following content to the example.js file.
In this example, the project listens on port 3000 and returns Hello World. You can change the project content (res.end), port number (const port), and other settings 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 plain text. 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 for compatibility with older browsers. res.setHeader('Pragma', 'no-cache'); // Set the Expires header to a past date to make the content expire immediately. You can also use 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}/`); });After you add the code, press Esc to exit Insert mode. Then, enter
:wqand press Enter to save the file and exit.
Run the project.
node ~/example.jsRun the following command to list the listening ports.
netstat -tplnIf port 3000 is included in the output, the project is running correctly.
Add an inbound rule to the security group of your ECS instance to allow traffic on the project's port.
In this example, the project port is 3000. For details about how to add a security group rule, see Add a security group rule.
Set the protocol to Custom TCP and the source to
0.0.0.0/0(all IPv4 addresses).In a web browser, navigate to
http://.: In this example, the <project_port_number> is 3000. If the deployment is successful, the page returns the following content:
Hello World
Windows systems
To deploy Node.js on a Windows system, download the official installer from the Node.js website.
Install Node.js
Connect to the instance where you want to install Node.js. For details, see Connect to a Windows instance by using Workbench.
Go to the official Node.js website and click **Download** in the menu bar.
On the Prebuilt Installer tab, set **Version** to v20.13.1 (LTS), **OS** to Windows, and **Arch** to x64**. Then, click Download Node.js v20.13.1.
Download the installer.
This guide uses the Prebuilt Installer method.
NoteWe recommend choosing a version labeled LTS (Long-Term Support) because it has been tested over a longer period and is more stable.
Installation method
Description
Package manager
Installs Node.js by using the operating system's package manager, such as npm, apt, yum, or brew.
Prebuilt installer
A pre-compiled installer for a specific operating system, such as a .msi file for Windows or a .pkg file for macOS. These installers include the Node.js runtime, npm, and typically all necessary dependencies.
Prebuilt binaries
Pre-compiled Node.js executable files for various platforms. This method does not have an installation wizard and requires you to manually extract the files and configure environment variables.
Source code
Requires compiling from the source code. After extracting the files, you must use specific scripts to configure, compile, and install it.
Double-click the downloaded installer and follow the setup wizard to complete the installation.
Open Command Prompt (CMD) or PowerShell.
Run
node -vandnpm -v. The following output confirms a successful installation.PS C:\Users\Administrator> node -v v22.12.0 PS C:\Users\Administrator> npm -v 10.9.0 PS C:\Users\Administrator>
Deploy a test project
Create a project folder named newfolder and a text file named example.txt.
In this example, the folder path is
C:\Users\Administrator\newfolder.Double-click to open the example.txt file and paste the following content into it.
In this example, the project listens on port 3000 and returns Hello World. You can change the project content (res.end), port number (const port), and other settings 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 plain text. 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 (for compatibility with older browsers). res.setHeader('Pragma', 'no-cache'); // Set the Expires header to a past date to make the content expire immediately. Or use 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}/`); });Change the file extension of example.txt to example.js. Change the extension of the file
example.txtfrom.txtto.js. The filename becomesexample.js, and the file type is displayed as a JavaScript file.Open Command Prompt (CMD) or PowerShell. Navigate to the newfolder directory and run the
npm initcommand to initialize the project. Follow the prompts to enter the project information and generate apackage.jsonfile.C:\Users\Administrator\newfolder>npm init This utility will walk you through creating a package.json file. It only covers the most common items, and tries to guess sensible defaults. See `npm help init` for definitive documentation on these fields and exactly what they do. Use `npm install <pkg>` afterwards to install a package and save it as a dependency in the package.json file. Press ^C at any time to quit. package name: (newfolder) example version: (1.0.0) 1.0.0 description: demo entry point: (example.js) example.jsRun
node example.jsto start the project. A confirmation message appears in the command-line window, indicating the server is running.C:\Users\Administrator\newfolder>node example.js Server running at http://0.0.0.0:3000/In the security group of the ECS instance, add an inbound rule to allow traffic on the port configured for the project, which is port 3000 in this example. For details, see Add a security group rule.
On your local computer or any computer with public internet access, open a web browser and go to
http://.: In this example, the <project_port_number> is 3000. If the deployment is successful, the page returns the following content:
Hello World