Beginner's Guide: Creating a "Hello World" Program with Node.js

Beginner's Guide: Creating a "Hello World" Program with Node.js

·

2 min read

1. Introduction

  • The video explains how to write a "Hello World" program in Node.js.

  • Node.js is a JavaScript runtime environment that allows running JavaScript outside a browser.

2. Setting Up the Project

  • Create an empty folder (e.g., NodeJS).

  • Inside that folder, create a subfolder named HelloWorld to keep the project organized.

  • Open the folder in a code editor (e.g., VS Code).

3. Writing the First Node.js Script

  • Create a new file named hello.js.

  • Inside hello.js, write:

      console.log("Hello, World!");
    
  • console.log prints output to the terminal.

4. Running the Script

  • Open the terminal in the project folder.

  • Run the script using the command:

      node hello.js
    
  • The output should be:

      Hello, World!
    

5. Difference Between Node.js and Browser JavaScript

  • Node.js does not support browser-specific objects like window and document because it runs JavaScript on the server, not in a browser.

  • Example:

      console.log(window);
    

    Running this in Node.js will cause an error: window is not defined.

  • However, in the browser console, window is available.

6. Node.js Runtime Features

  • Node.js uses the V8 engine, but it removes browser-specific features.

  • It adds server-side capabilities such as:

    • Cryptography (Encryption & Hashing)

    • File handling

    • Package management using npm (Node Package Manager)

7. Installing and Checking Node.js

  • Verify if Node.js is installed using:

      node -v
    
  • npm (Node Package Manager) is installed alongside Node.js.

  • Check npm version using:

      npm -v
    
  • npm is used for managing dependencies in a Node.js project.

8. Initializing a Node.js Project

  • Run the following command to initialize a Node.js project:

      npm init
    
  • This creates a package.json file, which stores project metadata and dependencies.

9. Summary

  • Node.js is used to execute JavaScript outside the browser.

  • It removes browser-specific features but adds server-side functionalities.

  • The first Node.js program logs "Hello, World!" to the console.

  • Use npm for package management in Node.js projects.