Technology Encyclopedia Home >How to install and use Node.js on Pop!_OS?

How to install and use Node.js on Pop!_OS?

To install and use Node.js on Pop!_OS, follow these steps:

Installation

  1. Update Your System:
    First, ensure your system is up to date.

    sudo apt update && sudo apt upgrade
    
  2. Install Node.js:
    You can install Node.js using the NodeSource repository, which provides the latest versions.

    curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
    sudo apt-get install -y nodejs
    
  3. Verify Installation:
    Check if Node.js and npm (Node Package Manager) are installed correctly.

    node -v
    npm -v
    

Usage

  1. Create a Project Directory:
    Make a new directory for your project and navigate into it.

    mkdir my-node-project
    cd my-node-project
    
  2. Initialize a New Node.js Project:
    Initialize a new project with default settings.

    npm init -y
    
  3. Install Packages:
    Install any packages you need via npm. For example, to install Express.js:

    npm install express
    
  4. Create a Simple Server:
    Create a file named index.js and add the following code to set up a basic Express server:

    const express = require('express');
    const app = express();
    const port = 3000;
    
    app.get('/', (req, res) => {
      res.send('Hello World!');
    });
    
    app.listen(port, () => {
      console.log(`Server running at http://localhost:${port}/`);
    });
    
  5. Run Your Server:
    Start your server using Node.js.

    node index.js
    

    Open your browser and go to http://localhost:3000 to see "Hello World!".

Additional Resources

For more advanced usage and best practices, consider exploring the official Node.js documentation and tutorials. Additionally, if you're working on a cloud-based project, services like Tencent Cloud offer robust platforms for deploying and managing Node.js applications. Their Cloud Base service provides a seamless experience for developing, deploying, and scaling applications, supporting Node.js among other languages and frameworks.