# Node.js
- Node.js is an open-source server environment built on Chrome's V8 JavaScript engine.
- Node.js is free and runs on various platforms (Windows, Linux, Unix, Mac OS X).
# Installation guides
# Install node.js
Go to https://nodejs.org/en/download (opens new window). Download and install the latest LTS version of Node. Node.js is updated frequently, so a newer LTS version may be available by the time you read this.
Follow the default settings when you install Node. To check your version, open a new Git Bash window and enter:
node --version
or node -v
.
$ node -v v22.12.0
Copied!
2
REMARKS
In this course we don't write node scripts ourselves. We only use ready-made scripts like live-server. Besides node.js, two other packages are installed by default:
- npm: or the node package manager for installing one of the 1.3 million open source packages (opens new window) for node
- npx: or the node package runner for running (executing) a locally installed package, which is beyond the scope of this course
# Semantic Versioning
Node.js (and other packages) uses Semantic Versioning (SemVer), a versioning system that follows the
format x.y.z
to indicate the type of changes in each release. This system helps developers understand the impact
of updates and manage dependencies effectively.
# Breakdown of Semantic Versioning:
- Major Version (
x
): Indicates breaking changes that are not backward compatible. For example, upgrading from Node.js21.x.x
to22.x.x
may require changes to your code. - Minor Version (
y
): Adds new features in a backward-compatible way. For example, upgrading from Node.js22.10.x
to22.12.x
introduces new functionality without breaking existing code. - Patch Version (
z
): Includes backward-compatible bug fixes or security updates. For example, upgrading from Node.js22.12.0
to22.12.1
resolves issues without affecting functionality.
# Example:
If your project uses Node.js 22.12.0
:
- A patch update to
22.12.1
is safe and fixes bugs. - A minor update to
22.13.0
adds features but doesn’t break your code. - A major update to
23.0.0
may introduce breaking changes, requiring you to test and update your code.
For more details, visit the Node.js release schedule (opens new window) to see which versions are actively maintained.
# Install live-server
- Live-server is a development server with live reload capability for HTML, CSS and JavaScript files
- Live-server only works for static sites (not for PHP, ...)
Install live-server globally:
- open a terminal window and enter
npm i -g live-server
- open a new Git Bash window and enter:
live-server --version
orlive-server -v
$ live-server -v live-server 1.2.2
Copied!
2
Browse to the folder where your static site is stored and enter live-server
, e.g.
Your site opens in the default browser on http://127.0.0.1:8080 (opens new window) (or http://localhost:8080 (opens new window))
TIP
You can run multiple live servers simultaneously by setting different ports for each website
live-server
uses the default port 8080:http://127.0.0.1:8080
live-server --port=3000
uses port 3000:http://127.0.0.1:3000
# What is npm?
npm (Node Package Manager) is the default package manager for Node.js. npm consists of 2 parts:
- a command-line interface for downloading (and publishing, which is beyond the scope of this course) packages
- an online repository (opens new window) with 1.3 million JavaScript packages
On this page, we will explain the most important concepts of npm by examining the contents of the file package.json
READING TIPS
# package.json
Most projects contain a package.json file. This file contains the meta-data, scripts and dependencies of the project. This file is written in JSON format:
- JSON (opens new window) (or JavaScript Object Notation) is a lightweight text format to interchange data
- JSON is easy to understand and is based on the following syntax rules
- Data is represented in "name":"value" pairs
- Data is separated by commas
- Curly braces { } contain objects
- Square brackets [ ] contain arrays
# meta-data
- name, version, description, repository, keywords, author, license, bugs, ...
- Example package.json
{ "name": "css_shades_generator", "version": "1.0.0", "description": "CSS Custom Properties Shades Generator", "main": "", "repository": { "type": "git", "url": "git+https://github.com/pverhaert/css_shades_generator.git" }, "keywords": [ "Thomas More Kempen", "IT Factory", "Full Stack Essentials", "CSS Custom Properties", "Bootstrap" ], "author": "Peter Verhaert", "license": "ISC", "bugs": { "url": "https://github.com/pverhaert/css_shades_generator/issues" }, "homepage": "https://github.com/pverhaert/css_shades_generator#readme", "type": "module", "scripts": { ... }, "devDependencies": { ... } }
Copied!
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# scripts
- In package.json, you can also define scripts to run command-line tools (which are installed globally or in the project's local context)
- Example: package.json of css_bootstrap_demo_project (opens new window)
{ ... "scripts": { "watch": "cd src && live-server --port=6060" }, ... }
Copied!
2
3
4
5
6
7
These scripts can be launched
- using the commands
npm run <script-name>
in a terminal window - by double-clicking the corresponding scripts in PhpStorm's npm tool window (right-click within the editor window of package.json and choose Show npm Scripts)
- by clicking on the arrow ▶ before the scripts
# dependencies
- In package.json, you can also specify (
"devDependencies"
) which packages are needed locally for your project - Example: package.json of css_shades_generator (opens new window)
{ ... "scripts": { ... }, "devDependencies": { "autoprefixer": "^10.4.20", "postcss": "^8.4.49", "sweetalert2": "^11.15.9", "tailwindcss": "^3.4.17", "vite": "^6.0.7" } }
Copied!
2
3
4
5
6
7
8
9
10
11
12
13
When you open a project with a package.json file (for the first time), you should install the needed packages:
- by executing the command
npm install
(ornpm i
) in the terminal - by clicking on Run 'npm install' in the PhpStorm pop-up
The packages are installed (locally) in the folder node_modules.
REMARKS
The packages that are installed this way are only available in the current project (via the folder node_modules),
in contrast to the packages that we installed globally (using the -g
option), such as live-server, ...
If a package that is installed depends on other packages, these are installed as well, and this (process repeats itself)
until every needed package is installed. Therefore, if you look at the node_modules
folder in more detail, you notice that, besides the packages specified in "devDependencies"
, a huge amount of
other packages are installed.
If you want to use Git (version control) for your project, this node_modules folder should be added to
the .gitignore file as it can become rather large (e.g. ~37 Mb for the
css_shades_generator (opens new window)) and collaborators to the project can easily
install
these packages locally anyway.
Npm packages also follow the Semantic Versioning system, which is explained earlier in this chapter.
# Specifying Versions in package.json
When specifying Node.js or package versions in your package.json
file, you can use different strategies to control
how updates are applied:
- Exact Version: Specifying an exact version (e.g.,
"x.y.z"
) ensures that only that specific version will be installed. This provides maximum stability but prevents automatic updates, even for bug fixes or security patches. - Tilde (
~
): Using a tilde before the version (e.g.,"~x.y.z"
) allows updates to the latest patch version within the specified minor version. For example,"~22.12.0"
will allow updates to22.12.1
,22.12.2
, etc., but not to22.13.0
. - Caret (
^
): Using a caret before the version (e.g.,"^x.y.z"
) allows updates to the latest minor or patch version within the specified major version. For example,"^22.12.0"
will allow updates to22.13.0
,22.14.0
, etc., but not to23.0.0
.
# Example:
If your project uses a module with eg. version 5.8.0
:
- A patch update to
5.8.1
is safe and fixes bugs. - A minor update to
5.9.0
adds features but doesn’t break your code. - A major update to
6.0.0
may introduce breaking changes, requiring you to test and update your code.
TIP
Hover over the version specification of a package in package.json with the CTRL
key pressed:
# package-lock.json
- Describes (amongst other things) the exact versions of all installed packages and their dependencies, creating a snapshot of your project's dependency tree at a specific point in time.
- If your project includes a
package-lock.json
file, runningnpm install
will prioritize this file. npm will install the exact versions of packages listed inpackage-lock.json
, ignoring the version ranges specified in yourpackage.json
. This ensures that all team members and deployment environments install precisely the same versions of packages, leading to consistent and reproducible builds. - If this file doesn't exist, running
npm install
will install packages based on the version ranges specified in yourpackage.json
file.
# devDependencies vs dependencies
- Most of the dependencies are listed under
"devDependencies"
, as they are only needed during local **development ** - Packages that are needed for your application in production should be listed under
"dependencies"
REMARK: Starting from scratch
In the explanation above, we assumed your project already contains a package.json file. If you start a new project from scratch:
- Create a new package.json file using the command
npm init -y
- Install the necessary packages (as developer dependencies) in the folder node_modules using the commands
npm i <package-name>(@<version>) --save-dev
- Examples:
npm i package1 package2 package3 --save-dev
- These packages are added to (the
"devDependencies"
section in) package.json and package-lock.json
- Examples:
# Common npm commands
Command | Shorthand | Description |
---|---|---|
npm init -y | Create a new package.json file with default settings | |
npm install | npm i | Install all packages listed in package.json |
npm install <package> | npm i <package> | Install a package locally |
npm install <package-name> | npm i <package-name> | Install a specific version of a package |
npm install --save-dev <package> | npm i -D <package> | Install a package as a developer dependency |
npm install --global <package> | npm i -g <package> | Install a package globally |
npm list | List all locally installed packages | |
npm list -g | List all globally installed packages |