Introduction to Node.js

Brief History & Context

In 1995, Brendan Eich developed JavaScript in just 10 days while working at Netscape. Initially, JavaScript was intended to add interactivity to web pages and was executed only in browsers (like Netscape Navigator). For years, JavaScript was strictly a client-side scripting language.

However, developers began to see the potential of using JavaScript outside the browser — particularly on the server side.

That’s where Node.js comes in.

What is Node.js?

Node.js, created by Ryan Dahl in 2009, is a runtime environment that allows developers to run JavaScript code outside the browser, typically on the server.

  • Built on Google Chrome’s V8 engine, Node.js translates JavaScript code into machine code quickly and efficiently.
  • It uses an event-driven, non-blocking I/O model, making it fast and scalable, ideal for building real-time applications like chat apps, REST APIs, and data-intensive services.
In short, Node.js gave JavaScript “superpowers” — enabling full-stack development using a single language.

Creating a Node.js Project

Here’s a step-by-step guide to set up a simple Node.js project:

1. Install Node.js

Download from https://nodejs.org. It comes with npm (Node Package Manager).

To verify installation:

node -v
npm -v

2. Initialize a New Project

Open terminal and run:

mkdir my-node-app
cd my-node-app
npm init -y

This creates a package.json file with default settings.

3. Create an Entry File

Create index.js:

// index.js
console.log("Hello from Node.js!");

Run it:

node index.js

Understanding package.json

The package.json file is the metadata and config file for a Node.js project. It tells npm how to handle your project and its dependencies.

Example package.json:

{
  "name": "my-node-app",
  "version": "1.0.0",
  "description": "A simple Node.js app",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "author": "Your Name",
  "license": "MIT",
  "dependencies": {}
}

Key Fields Explained:

  • "name": The project name.
  • "version": Version of your app.
  • "description": Short summary of the project.
  • "main": The entry point file.
  • "scripts": Define commands like npm start.
  • "dependencies": Lists packages installed via npm install <package>.

Summary

  • JavaScript (1995) was browser-only and client-side.
  • Node.js (2009) brought JavaScript to the server, revolutionising full-stack development.
  • You can easily create a Node.js project with npm init and manage it via package.json.
Feedback Display

Learner Reviews