Node.js
Node.js lets you run JavaScript outside the browser.
It is mainly used for:
- web apps
- APIs
- scripts and tools
- real-time applications
Basics
Section titled “Basics”What is Node.js?
Section titled “What is Node.js?”Node.js is a runtime.
That means:
- JavaScript is the language
- Node.js is the environment that runs it on your computer or app
So instead of only running JavaScript in the browser, you can also use it for backend code.
Why It Is Popular
Section titled “Why It Is Popular”- one language for frontend and backend
- fast for I/O tasks like reading files, handling requests, and talking to databases
- huge package ecosystem with npm
- widely used for APIs and web apps
Project Setup
Section titled “Project Setup”When you install Node.js, you usually also get npm.
npm is the package manager for Node.js.
You use it to:
- install libraries
- run scripts
- manage project dependencies
To start a new project, you can create a package.json file with:
npm init -ypackage.json
Section titled “package.json”Most Node.js projects have a package.json file.
It contains information such as:
- project name
- version
- scripts
- dependencies
- devDependencies
Example:
{ "name": "my-app", "type": "module", "scripts": { "dev": "nodemon app.js", "start": "node app.js" }, "dependencies": { "express": "^5.0.0" }, "devDependencies": { "nodemon": "^3.0.0" }}Scripts
Section titled “Scripts”Scripts are commands saved inside package.json.
They help you run common tasks with short commands.
Example:
{ "scripts": { "dev": "nodemon app.js", "start": "node app.js" }}Run them with:
npm run devnpm run startCommon script names:
devfor developmentstartfor starting the apptestfor tests
Run a script with:
npm run devDependencies
Section titled “Dependencies”dependencies are packages your app needs to run.
Example:
expressmysql2dotenv
Install a normal dependency with:
npm install expressdevDependencies are packages you only need during development.
Example:
nodemoneslintprettier
Install a dev dependency with:
npm install -D nodemonSimple rule:
- if the app needs it in production, use
dependencies - if only the developer needs it, use
devDependencies
Running Your App
Section titled “Running Your App”console.log('Hello from Node.js');Run it with:
node app.js# ornpm run devCore Concepts
Section titled “Core Concepts”Modules
Section titled “Modules”In Node.js, you usually split code into modules.
Example:
import fs from 'node:fs';
console.log('File system module loaded');Some common built-in modules are:
fsfor filespathfor file pathshttpfor HTTP appsprocessfor process info and environment variables
Async Nature
Section titled “Async Nature”Node.js is very good at handling many tasks without blocking.
This is important for things like:
- HTTP requests
- database queries
- reading files
- calling external APIs
Example:
setTimeout(() => { console.log('Runs later');}, 1000);
console.log('Runs first');Terminal output:
Runs firstRuns laterConfiguration
Section titled “Configuration”Environment Variables
Section titled “Environment Variables”Node.js apps often use environment variables for configuration.
Example:
console.log(process.env.PORT);This is useful for values like:
- port numbers
- API keys
- database URLs
These values are usually different between:
- development
- testing
- production
A .env file is a simple file for storing environment variables.
Example:
PORT=3000DB_URL=mysql://user:password@localhost:3306/app_dbAPI_KEY=abc123In your Node.js code, you can read them with:
console.log(process.env.PORT);console.log(process.env.DB_URL);Node.js version > 20
Section titled “Node.js version > 20”From Node.js v20.6.0, you can load a .env file directly from the command line.
PORT=3000DB_HOST=localhostDB_USER=adminDB_PASSWORD=secretRun your app with:
node --env-file=.env app.jsThe values from .env are then available in process.env.
Node.js version < 20
Section titled “Node.js version < 20”For earlier Node.js versions, you usually need an external library to load .env files.
The dotenv library is a widely used solution for managing .env files.
It works across Node.js versions and is simple to set up.
Install it with:
npm install dotenvUse it in your application:
import 'dotenv/config';
console.log(`App is running on port ${process.env.PORT}`);The .env file is automatically loaded, and its variables are added to process.env.
.gitignore
Section titled “.gitignore”The .gitignore file tells Git which files should not be committed.
This is important for files that are:
- private
- generated automatically
- different on each machine
Common examples in Node.js projects:
node_modules/.envdist/Why these are ignored:
node_modules/can be installed again with npm.envmay contain secretsdist/is often generated output