Node.js lesson · 32 min

Node.js npm and package.json

Create a real package.json, add scripts, run npm commands, and understand dependencies.

Code runs locally in your browser

What you will practice

  • Create `package.json` with `npm init -y`.
  • Add and run npm scripts.
  • Understand dependencies, devDependencies, and lockfiles.

What this means

npm is the package manager bundled with Node.js installations. It installs packages and runs project scripts.

`package.json` is the project manifest. It describes the name, version, entry point, scripts, dependencies, devDependencies, and other metadata.

A package script is a named terminal command. For example, `npm run start` can run `node index.js`, and `npm test` can run your tests.

If this is your first time seeing this

npm helps Node.js projects use reusable packages.

package.json is the project description and command list.

Mini glossary

npm
A package manager commonly used with Node.js.
package.json
The JSON manifest describing a Node.js project.
dependency
A package your project needs to run or build.

Example from everyday life

package.json is like the label and instruction sheet inside a kit. It tells you what the kit is called, which parts it needs, and which buttons to press for common actions.

How it works step by step

  1. Create an empty folder and run `npm init -y`.
  2. Edit `package.json` and add scripts.
  3. Create `index.js` and run it with `npm run start`.
  4. Install packages only when you need them; each package becomes part of the project supply chain.

Where you will use this

  • A web app runs `npm run build` before deployment.
  • A backend project lists Express, database drivers, or testing libraries as dependencies.
  • A package maintainer defines scripts for linting and tests.

Before you run the code

The npm documentation describes `scripts` as a dictionary of commands and `dependencies` as a map of package names to version ranges.

In real projects, commit `package.json` and `package-lock.json` so installs are repeatable. Do not commit `node_modules`.

Run it locally

These examples are meant for a local Node.js project. Create the files shown below, then run the terminal commands from the project folder.

Create a project

mkdir lumio-npm-demo
cd lumio-npm-demo
npm init -y

Add scripts

package.json
{
  "name": "lumio-npm-demo",
  "version": "1.0.0",
  "type": "commonjs",
  "scripts": {
    "start": "node index.js",
    "check": "node --check index.js"
  }
}

Create the entry file

index.js
const startedAt = new Date().toISOString();

console.log("Lumio npm demo");
console.log(`Started at: ${startedAt}`);
console.log("Run this file through npm scripts.");

Run scripts

npm run check
npm run start

Optional package install example

Only run this when you intentionally want to add a dependency.

npm install zod
npm uninstall zod

Common beginner mistakes

  • Installing packages without checking whether they are maintained.
  • Forgetting to commit the lockfile in real projects.
  • Putting secrets inside package.json scripts.

Why there is no browser runner here

This lesson uses real Node.js APIs and terminal commands. Run the examples locally with Node.js so you learn the same workflow used in actual backend projects.

Try changing this next

  • Add a `build` script.
  • Add another dependency name.
  • Print the total number of dependencies.