In this section, we will guide you through the process of setting up Node.js on your machine. By the end of this module, you will have Node.js installed and ready to run your first program.

  1. Checking for Node.js Installation

Before installing Node.js, it's a good idea to check if you already have it installed on your system. Open your terminal or command prompt and type the following command:

node -v

If Node.js is installed, you will see the version number. If not, you will get an error message indicating that the command is not recognized.

  1. Downloading Node.js

To download Node.js, follow these steps:

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

  2. Choose the appropriate version: You will see two versions available for download:

    • LTS (Long Term Support): Recommended for most users, especially for production environments.
    • Current: Includes the latest features but may not be as stable as the LTS version.

    Node.js Download Page

  3. Download the installer: Click on the version you want to download. This will download the installer for your operating system (Windows, macOS, or Linux).

  1. Installing Node.js

Windows

  1. Run the installer: Double-click the downloaded .msi file to start the installation process.
  2. Follow the prompts: The installer will guide you through the installation steps. Make sure to check the box that says "Automatically install the necessary tools" if prompted.
  3. Verify the installation: Open a new command prompt and type node -v to check the installed version.

macOS

  1. Run the installer: Double-click the downloaded .pkg file to start the installation process.
  2. Follow the prompts: The installer will guide you through the installation steps.
  3. Verify the installation: Open a new terminal window and type node -v to check the installed version.

Linux

For Linux, you can use a package manager to install Node.js. Here are the commands for some popular distributions:

Ubuntu/Debian

sudo apt update
sudo apt install nodejs npm

CentOS/RHEL

sudo yum install nodejs npm

Fedora

sudo dnf install nodejs npm

After installation, verify the installation by typing node -v in your terminal.

  1. Installing Node.js via nvm (Node Version Manager)

Using nvm is a great way to manage multiple versions of Node.js on your machine. Here’s how to install and use nvm:

Installing nvm

  1. Download and install nvm: Open your terminal and run the following command:

    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
    
  2. Load nvm: Add the following lines to your .bashrc, .zshrc, or .profile file:

    export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
    
  3. Restart your terminal: Close and reopen your terminal or run source ~/.bashrc (or the appropriate file for your shell).

Using nvm

  1. Install a specific version of Node.js: For example, to install Node.js version 14, run:

    nvm install 14
    
  2. Use a specific version of Node.js: To switch to the installed version, run:

    nvm use 14
    
  3. Verify the installation: Check the installed version by running:

    node -v
    

  1. Installing npm (Node Package Manager)

npm is automatically installed with Node.js. You can verify the installation by running:

npm -v

Conclusion

You have now successfully set up Node.js on your machine. In the next section, we will write and run your first Node.js program. This setup will serve as the foundation for all the upcoming modules in this course. Happy coding!

Node.js Course

Module 1: Introduction to Node.js

Module 2: Core Concepts

Module 3: File System and I/O

Module 4: HTTP and Web Servers

Module 5: NPM and Package Management

Module 6: Express.js Framework

Module 7: Databases and ORMs

Module 8: Authentication and Authorization

Module 9: Testing and Debugging

Module 10: Advanced Topics

Module 11: Deployment and DevOps

Module 12: Real-World Projects

© Copyright 2024. All rights reserved