Skip to main content
Node.js & Express·Lesson 1 of 5

Node.js Basics

Node.js is a JavaScript runtime built on Chrome's V8 engine. It lets you run JavaScript outside the browser, making it possible to build servers, command-line tools, and backend services with the same language you use on the frontend.

What Makes Node.js Different

In the browser, JavaScript has access to the DOM, window, and fetch. In Node.js, those do not exist. Instead, you get access to the file system, network, operating system, and other server-side APIs.

FeatureBrowserNode.js
DOM accessYesNo
File systemNoYes (fs module)
HTTP serverNoYes (http module)
window objectYesNo (global instead)
Module systemES ModulesCommonJS + ES Modules
Package managerN/Anpm, pnpm, yarn

Installing Node.js

Download the LTS version from nodejs.org. Verify the installation:

node --version   # e.g., v22.0.0
npm --version    # e.g., 10.0.0

Running JavaScript Files

Create a file called app.js:

const message = "Hello from Node.js!";
console.log(message);
console.log(`Running on ${process.platform}`);
console.log(`Node version: ${process.version}`);

Run it:

node app.js

The Module System

Node.js uses CommonJS modules by default. Each file is its own module.

Exporting from a Module

Ctrl+Enter
HTML
CSS
JS
Preview

Importing a Module

Ctrl+Enter
HTML
CSS
JS
Preview

ES Modules in Node.js

You can use ES module syntax by either setting "type": "module" in package.json or using the .mjs extension:

Ctrl+Enter
HTML
CSS
JS
Preview

Built-in Modules

Node.js ships with many useful modules. No installation required.

fs — File System

Ctrl+Enter
HTML
CSS
JS
Preview

path — File Paths

Ctrl+Enter
HTML
CSS
JS
Preview

os — Operating System Info

Ctrl+Enter
HTML
CSS
JS
Preview

npm and Package Management

npm (Node Package Manager) manages third-party packages.

Initializing a Project

mkdir my-project && cd my-project
npm init -y

This creates a package.json file that tracks your project's dependencies and scripts.

Installing Packages

npm install express        # production dependency
npm install -D nodemon     # development dependency

package.json Scripts

{
  "name": "my-project",
  "scripts": {
    "start": "node app.js",
    "dev": "nodemon app.js"
  },
  "dependencies": {
    "express": "^4.18.0"
  },
  "devDependencies": {
    "nodemon": "^3.0.0"
  }
}

Run scripts with:

npm run dev
npm start

Environment Variables

Store configuration outside your code using environment variables.

Ctrl+Enter
HTML
CSS
JS
Preview

Use a .env file with the dotenv package:

npm install dotenv
# .env
PORT=4000
DATABASE_URL=mongodb://localhost:27017/myapp
SECRET_KEY=super-secret-value
require("dotenv").config();
console.log(process.env.DATABASE_URL);

Never commit .env files to version control. Add .env to your .gitignore.

Creating a Simple HTTP Server

Node.js can create servers without any framework:

Ctrl+Enter
HTML
CSS
JS
Preview

This works but gets unwieldy fast. That is why frameworks like Express exist.

Practical Exercise

Build a file-based note-taking CLI tool:

Ctrl+Enter
HTML
CSS
JS
Preview

Key Takeaways

  • Node.js lets you run JavaScript on the server with access to the file system, network, and OS.
  • Use require (CommonJS) or import (ES Modules) to organize code into modules.
  • npm manages packages and project scripts through package.json.
  • Environment variables keep secrets and configuration out of your source code.
  • The built-in http module can create servers, but Express makes it much easier.