npm and package.json
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
mkdir course-api
cd course-api
npm init -y-y accepts the defaults. Then edit the file to suit an application:
{
"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:
npm run devstart and test are special and work without run:
npm start
npm testWhy scripts matter
- New teammates don’t need to know which tools you use —
npm run devjust works. - CI servers and hosting platforms run
npm testandnpm startby 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:
{
"scripts": {
"lint": "eslint .",
"format": "prettier --write ."
}
}Chaining and passing arguments
{
"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 --:
npm test -- --test-name-pattern="login"pre and post scripts
A script named pre<name> runs automatically before <name>, and post<name> after it:
{
"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.
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 wayGlobal 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
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:
node_modules/
.env
coverage/
*.logKeep
package.jsontidy. It’s the first file a new developer opens. A good description, anenginesfield and clear script names tell them how the project works before they read any code.
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.
{
"name": "course-api"
}{
"name": "course-api",
"version": "1.0.0",
"private": true,
"type": "module",
"engines": { "node": ">=22" },
"scripts": {
"start": "node src/server.js",
"dev": "node --watch src/server.js",
"lint": "eslint .",
"test": "node --test",
"check": "npm run lint && npm test"
}
}Summary
package.jsondescribes your project: its name, module type, scripts and dependencies.npm init -ycreates one; set"type": "module"and"private": truefor 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. preandpostscripts, and&&, chain steps together.