Instagram
youtube
Facebook
Twitter

Setting Up Node.js Environment

Introduction

Setting up the Node.js environment is the first step towards building applications using this powerful runtime. This lesson will guide you through the installation process, configuration, and verification of your Node.js setup.


System Requirements

Before installing Node.js, ensure your system meets the following requirements:

  • Operating System: Windows, macOS, or Linux.
  • Hardware: A machine with at least 1GB of RAM (more is recommended for larger applications).

Step 1: Downloading Node.js

  1. Visit the Official Website: Go to the Node.js official website.
  2. Choose the Right Version: You will see two versions available for download:
    • LTS (Long Term Support): Recommended for most users as it receives regular updates and stability improvements.
    • Current: Contains the latest features but may not be as stable.
  3. Download the Installer: Click on the installer for your operating system (Windows, macOS, or Linux).

Step 2: Installing Node.js

  • Windows Installation:
    1. Run the downloaded .msi installer.
    2. Follow the setup wizard instructions, accepting the license agreement and default settings.
    3. Ensure the option to install npm (Node Package Manager) is checked.
  • macOS Installation:
    1. Open the downloaded .pkg file.
    2. Follow the on-screen instructions to complete the installation.
  • Linux Installation:
    • For most Linux distributions, you can install Node.js using a package manager. Here’s how to do it for Ubuntu:
    • Alternatively, you can use Node Version Manager (nvm) to install Node.js, which allows for easier management of multiple Node.js versions.
    sudo apt update
    sudo apt install nodejs npm
    

Step 3: Verifying the Installation

To ensure Node.js and npm were installed correctly, follow these steps:

  1. Open a terminal (Command Prompt on Windows, Terminal on macOS/Linux).
  2. Check the Node.js version by typing:
    node -v
    
    You should see the installed version number displayed.
  3. Check the npm version by typing:
    npm -v
    
    This will display the npm version number.

Step 4: Setting Up a Simple Node.js Project

  1. Create a Project Directory:

    • Navigate to your preferred location in the terminal and create a new directory:
    mkdir my-node-app
    cd my-node-app
    
  2. Initialize the Project:

    • Run the following command to create a package.json file, which will keep track of your project’s dependencies:
    npm init -y
    

    The -y flag automatically accepts the default configuration.

  3. Create Your First JavaScript File:

    • Create a new file named app.js using a text editor of your choice (e.g., Visual Studio Code, Sublime Text):
    touch app.js
    
  4. Write Your First Node.js Code:

    • Open app.js and add the following code to create a simple web server:
    const http = require('http');
    
    const hostname = '127.0.0.1';
    const port = 3000;
    
    const server = http.createServer((req, res) => {
        res.statusCode = 200;
        res.setHeader('Content-Type', 'text/plain');
        res.end('Hello, Node.js!\n');
    });
    
    server.listen(port, hostname, () => {
        console.log(`Server running at http://${hostname}:${port}/`);
    });
    
  5. Run Your Node.js Application:

    • In the terminal, run the following command to start your server:
    • You should see a message indicating that the server is running.
    node app.js
    
  6. Access Your Application:

    • Open your web browser and navigate to http://127.0.0.1:3000/. You should see the message "Hello, Node.js!"

Conclusion

In this lesson, you learned how to set up the Node.js environment on your machine, verify the installation, and create a simple Node.js application. This foundational knowledge will serve as the basis for building more complex applications in the following lessons.


Key Takeaways

  • Node.js can be easily installed on various operating systems.
  • The npm package manager is included in the Node.js installation.
  • You can create and run a basic Node.js application to verify your setup.