tencent cloud

Feedback

Manual Setup of Node.js

Last updated: 2024-04-28 14:49:39

    Introduction

    This article describes how to deploy Node.js on a CVM and create a sample project.
    To do this, you need to be familiar with common Linux commands such as Installing Software via YUM in a CentOS Environment and understand the versions of the installed software.

    Software

    Setting up Node.js involves:
    CentOS: a distribution of the Linux operating system. We use CentOS 7.9 in this article.
    Node.js: a JavaScript runtime environment. We use Node.js 16.10.0 and Node.js 13.10.0 in this article.
    npm: a package manager for JavaScript. We use npm 13.10.0 in this article to manage multiple Node.js versions.

    Prerequisites

    To set up Node.js, you need a Linux CVM. If you have not purchased one yet, see Getting Started with Linux CVMs.

    Directions

    Step 1: Logging in to a Linux instance

    Log in to a Linux instance using WebShell (recommended). You can also use other login methods that you are comfortable with:

    Step 2: Installing Node.js

    1. Run the following command to download the Node.js 64-bit install package for Linux.
    wget https://nodejs.org/dist/v16.10.0/node-v16.10.0-linux-x64.tar.xz
    Note:
    Visit the Node.js official website for more information.
    2. Run the following command to decompress the install package.
    tar -xf node-v16.10.0-linux-x64.tar.xz
    3. Run the following commands to create symbolic links.
    ln -s /root/node-v10.16.3-linux-x64/bin/node /usr/local/bin/node
    ln -s /root/node-v16.10.0-linux-x64/bin/npm /usr/local/bin/npm
    Once created, you are able to use node and npm commands in any CVM directory.
    4. Run the following commands to view Node.js and npm versions.
    node -v
    npm -v

    Step 3: Installing multiple Node.js versions (optional)

    Note:
    This process allows you to install multiple Node.js versions. Developers can use this to quickly switch among versions.
    1. Run the following command to install git.
    yum install -y git
    2. Run the following command to download the NVM source code and check for the newest version.
    git clone https://github.com/cnpm/nvm.git ~/.nvm && cd ~/.nvm && git checkout `git describe --abbrev=0 --tags`
    3. Run the following to configure NVM environment variables.
    echo ". ~/.nvm/nvm.sh" >> /etc/profile
    4. Run the following command to read system environment variables.
    source /etc/profile
    5. Run the following commands to view all Node.js versions.
    nvm list-remote
    6. Run the following commands to install multiple Node.js versions.
    nvm install v13.10.0
    nvm install v16.10.0
    7. Run the following command to view all installed Node.js versions.
    nvm ls
    If the following appears, then the installation is successful and the current version in use isNode.js 16.10.0.
    
    
    8. Run the following command switch to another version.
    nvm use v13.10.0
    The following appears:
    
    

    Step 4: Creating a sample project

    1. Run the following commands to create a file named index.js under the root path.
    cd ~
    vim index.js
    2. Press i to enter edit mode and input the following in the index.js file:
    const http = require('http');
    const hostname = '0.0.0.0';
    const port = 7500;
    const server = http.createServer((req, res) => {
    if (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}/`);
    });
    Note:
    This article uses port 7500 in the index.js file. You can use other ports as needed.
    3. Press Esc and input :wq to save the file and go back.
    4. Run the following command to execute the Node.js project we just created.
    node index.js
    5. Open a browser window on your local machine and visit the following URL to check if the project has been executed successfully.
    http://CVM_Public_IP:Port
    If the following appears, Node.js is installed successfully.
    
    

    FAQ

    If you encounter a problem when using CVM, refer to the following documents for troubleshooting based on your actual situation.
    For issues regarding CVM login, see Password Login and SSH Key Login and Login and Remote Access.
    For issues regarding the CVM network, see IP Addresses and Ports and Security Groups.
    For issues regarding CVM disks, see System and Data Disks.
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support