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.
| Feature | Browser | Node.js |
|---|---|---|
| DOM access | Yes | No |
| File system | No | Yes (fs module) |
| HTTP server | No | Yes (http module) |
window object | Yes | No (global instead) |
| Module system | ES Modules | CommonJS + ES Modules |
| Package manager | N/A | npm, 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.0Running 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.jsThe Module System
Node.js uses CommonJS modules by default. Each file is its own module.
Exporting from a Module
Importing a Module
ES Modules in Node.js
You can use ES module syntax by either setting "type": "module" in package.json or using the .mjs extension:
Built-in Modules
Node.js ships with many useful modules. No installation required.
fs — File System
path — File Paths
os — Operating System Info
npm and Package Management
npm (Node Package Manager) manages third-party packages.
Initializing a Project
mkdir my-project && cd my-project
npm init -yThis 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 dependencypackage.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 startEnvironment Variables
Store configuration outside your code using environment variables.
Use a .env file with the dotenv package:
npm install dotenv# .env
PORT=4000
DATABASE_URL=mongodb://localhost:27017/myapp
SECRET_KEY=super-secret-valuerequire("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:
This works but gets unwieldy fast. That is why frameworks like Express exist.
Practical Exercise
Build a file-based note-taking CLI tool:
Key Takeaways
- Node.js lets you run JavaScript on the server with access to the file system, network, and OS.
- Use
require(CommonJS) orimport(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
httpmodule can create servers, but Express makes it much easier.