npm and package.json

Topic 3 of 4 in Modules & packages6 min readIntermediateFree course

Overview

Every Node.js project has a package.json file in its root folder. It’s the project’s identity card and control panel: it names the project, says which module system it uses, lists the packages it depends on, and defines the commands people run.

You met npm briefly in JavaScript Essentials. Now you’ll use it the way professional teams do.

Creating package.json

Terminal
mkdir course-api
cd course-api
npm init -y

-y accepts the defaults. Then edit the file to suit an application:

JSON
{
  "name": "course-api",
  "version": "1.0.0",
  "description": "REST API for the academy",
  "private": true,
  "type": "module",
  "main": "src/server.js",
  "engines": { "node": ">=22" },
  "scripts": {
    "start": "node src/server.js",
    "dev": "node --watch --env-file=.env src/server.js",
    "test": "node --test"
  },
  "license": "UNLICENSED"
}

The important fields

Field What it’s for
name the package name — lowercase, no spaces
version semantic version (MAJOR.MINOR.PATCH)
private true stops it being published to npm by accident — always set it for apps
type "module" makes .js files ES modules
main / exports the entry file other code gets when it imports this package
engines which Node versions it supports
scripts named commands
dependencies packages needed at runtime
devDependencies packages needed only for development and testing

Libraries you publish need more care with exports, files and versioning, but for applications the fields above are all you need.

npm scripts

Scripts give a project a shared vocabulary of commands. Instead of remembering node --watch --env-file=.env src/server.js, everyone types:

Terminal
npm run dev

start and test are special and work without run:

Terminal
npm start
npm test

Why scripts matter

  • New teammates don’t need to know which tools you use — npm run dev just works.
  • CI servers and hosting platforms run npm test and npm start by convention.
  • Local tools are on the path. A package installed in the project (like eslint) can be called by name inside a script, without installing it globally:
JSON
{
  "scripts": {
    "lint": "eslint .",
    "format": "prettier --write ."
  }
}

Chaining and passing arguments

JSON
{
  "scripts": {
    "check": "npm run lint && npm test",
    "db:migrate": "node scripts/migrate.js",
    "db:seed": "node scripts/seed.js",
    "db:reset": "npm run db:migrate && npm run db:seed"
  }
}

&& runs the next command only if the previous one succeeded. Pass extra arguments to the underlying command after --:

Terminal
npm test -- --test-name-pattern="login"

pre and post scripts

A script named pre<name> runs automatically before <name>, and post<name> after it:

JSON
{
  "scripts": {
    "pretest": "npm run lint",
    "test": "node --test"
  }
}

Use this sparingly — explicit chains with && are easier for newcomers to follow.

npx

npx runs a command from a package: the copy in your project if there is one, or a temporary download if not.

Terminal
npx eslint --init        # run the project's eslint
npx serve public         # serve a folder without installing anything
npx create-vite@latest   # project generators are usually run this way

Global installs

npm install -g <package> installs a command for your whole machine. Keep these to a minimum — a project should declare its own tools in devDependencies so everyone uses the same versions. Good candidates for global installs are tools unrelated to any project, such as a deployment CLI.

A typical project layout

Text
course-api/
├── package.json
├── package-lock.json
├── .nvmrc
├── .env.example
├── .gitignore
├── src/
│   ├── server.js
│   └── …
├── test/
└── node_modules/        (not committed)

A .gitignore for Node projects should include at least:

Text
node_modules/
.env
coverage/
*.log

Keep package.json tidy. It’s the first file a new developer opens. A good description, an engines field and clear script names tell them how the project works before they read any code.

Exercise

Set up a project

Create a project folder with a package.json that: uses ES modules, requires Node 22 or newer, has start and dev scripts for src/server.js, and a check script that runs both lint and test.

JSON
{
  "name": "course-api"
}

Summary

  • package.json describes your project: its name, module type, scripts and dependencies.
  • npm init -y creates one; set "type": "module" and "private": true for apps.
  • Scripts give everyone the same commands: npm start, npm test, npm run dev.
  • Tools installed in the project run by name inside scripts, or with npx.
  • pre and post scripts, and &&, chain steps together.