Skip to content
UNASPACE

Node.js lets you run JavaScript outside the browser.

It is mainly used for:

  • web apps
  • APIs
  • scripts and tools
  • real-time applications


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.


  • 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


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:

Terminal window
npm init -y

Most Node.js projects have a package.json file.

It contains information such as:

  • project name
  • version
  • scripts
  • dependencies
  • devDependencies

Example:

package.json
{
"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 are commands saved inside package.json.

They help you run common tasks with short commands.

Example:

package.json
{
"scripts": {
"dev": "nodemon app.js",
"start": "node app.js"
}
}

Run them with:

Terminal window
npm run dev
npm run start

Common script names:

  • dev for development
  • start for starting the app
  • test for tests

Run a script with:

Terminal window
npm run dev

dependencies are packages your app needs to run.

Example:

  • express
  • mysql2
  • dotenv

Install a normal dependency with:

Terminal window
npm install express

devDependencies are packages you only need during development.

Example:

  • nodemon
  • eslint
  • prettier

Install a dev dependency with:

Terminal window
npm install -D nodemon

Simple rule:

  • if the app needs it in production, use dependencies
  • if only the developer needs it, use devDependencies

app.js
console.log('Hello from Node.js');

Run it with:

Terminal window
node app.js
# or
npm run dev


In Node.js, you usually split code into modules.

Example:

app.js
import fs from 'node:fs';
console.log('File system module loaded');

Some common built-in modules are:

  • fs for files
  • path for file paths
  • http for HTTP apps
  • process for process info and environment variables

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:

app.js
setTimeout(() => {
console.log('Runs later');
}, 1000);
console.log('Runs first');

Terminal output:

Runs first
Runs later


Node.js apps often use environment variables for configuration.

Example:

app.js
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:

.env
PORT=3000
DB_URL=mysql://user:password@localhost:3306/app_db
API_KEY=abc123

In your Node.js code, you can read them with:

app.js
console.log(process.env.PORT);
console.log(process.env.DB_URL);

From Node.js v20.6.0, you can load a .env file directly from the command line.

.env
PORT=3000
DB_HOST=localhost
DB_USER=admin
DB_PASSWORD=secret

Run your app with:

Terminal window
node --env-file=.env app.js

The values from .env are then available in process.env.

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:

Terminal window
npm install dotenv

Use it in your application:

app.js
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.


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:

.gitignore
node_modules/
.env
dist/

Why these are ignored:

  • node_modules/ can be installed again with npm
  • .env may contain secrets
  • dist/ is often generated output


Built with passion by Ngineer Lab