Google:

adverisment

Step-by-Step Guide to Installing Node.js On Your System

details img

Step 1: Download Node.js

  1. Visit the Official Node.js Website: Go to https://nodejs.org.

  2. Choose the Version to Install: You’ll typically see two options:

    • LTS (Long Term Support): This is the recommended version for most users, as it is stable and well-tested.
    • Current: The latest features and updates, but might not be as stable as the LTS version. Choose the LTS version unless you need the latest features.
  3. Download Node.js: Click on the appropriate version for your operating system (Windows, macOS, or Linux).


Step 2: Install Node.js

Windows and macOS

  1. Run the Installer: Once the download is complete, open the installer. The installer will guide you through the installation process with the following steps:

    • Accept the license agreement.
    • Choose the destination folder (the default should work fine).
    • Make sure the "Add to PATH" option is selected (this will allow you to use Node.js from the command line).
    • Proceed with the installation.
  2. Verify Installation: After installation, open your Command Prompt (Windows) or Terminal (macOS) and type the following commands to check if Node.js is installed correctly.

    bash
    node --version

    This will return the Node.js version installed. If it returns a version number, the installation is successful.

    Also, check for npm (Node Package Manager) which is bundled with Node.js:

    bash
    npm --version

    If both commands return version numbers, Node.js and npm are installed correctly.


Linux

On Linux, you can install Node.js using a package manager. The process varies depending on the distribution.

For Ubuntu/Debian-based systems:
  1. Update Package Index: Open a terminal and update the package index:

    bash
    sudo apt update
  2. Install Node.js: Install Node.js and npm using the following command:

    bash
    sudo apt install nodejs npm
  3. Verify Installation: After the installation is complete, verify that Node.js is installed by running:

    bash
    node --version

    And to check npm:

    bash
    npm --version
For Red Hat/CentOS-based systems:
  1. Install Node.js: First, you may need to install the NodeSource repository for Node.js. Run the following commands:

    bash
    curl -sL https://rpm.nodesource.com/setup_16.x | sudo -E bash - sudo yum install -y nodejs

    Replace 16.x with the version you want to install.

  2. Verify Installation: Check Node.js and npm versions:

    bash
    node --version npm --version

Step 3: Install Node.js Packages (Optional)

Once Node.js is installed, you can start using the npm package manager to install packages for your projects. For example, to install Express, a popular web framework for Node.js:

bash
npm install express

Step 4: Create Your First Node.js Project (Optional)

  1. Create a Project Directory: Create a new folder for your Node.js project:

    bash
    mkdir my-node-app cd my-node-app
  2. Initialize the Project: Initialize a new Node.js project with the following command:

    bash
    npm init

    This will create a package.json file where your project dependencies will be listed.

  3. Create a Simple Server: Create a file called app.js inside your project folder with the following content to create a basic server:

    javascript
    const http = require('http'); const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello, World!'); }); server.listen(3000, '127.0.0.1', () => { console.log('Server running at http://127.0.0.1:3000/'); });
  4. Run the Server: Run the server using the following command:

    bash
    node app.js

    You should see:

    arduino
    Server running at http://127.0.0.1:3000/

    Open a web browser and go to http://127.0.0.1:3000/. You should see "Hello, World!" displayed in your browser.


Step 5: Update Node.js (Optional)

To keep Node.js updated, you can use the following commands:

Windows/macOS:

Download and install the newer version from the Node.js website.

Linux (Ubuntu/Debian-based):

  1. Update the repository:

    bash
    sudo apt update
  2. Upgrade Node.js:

    bash
    sudo apt upgrade nodejs

Leave Comment