A complete guide to package.json

Explore the core fields in package.json and understand the roles of dependencies, devDependencies, scripts, and engines.

🎯 Introduction

In this article, you will learn to read package.json with confidence. Beyond identifying a project, it is the central configuration file for dependencies, scripts, and environment requirements. Understanding it helps you get started quickly with an unfamiliar project.


🧱 What is package.json?

package.json is the core configuration file in an NPM project, usually located in the project root. It has three main roles:

  1. Describe the project: its name, version, author, license, and other details.
  2. Manage dependencies: list the third-party packages the project needs and their version requirements.
  3. Define scripts: wrap common commands in short instructions such as npm run xxx.
Think of package.json as the project's menu. Anyone picking up the project can see which ingredients (dependencies) they need and how to prepare the dish (run scripts).

📋 Common fields explained

name and version

package.json
{
    "name": "my-project",
    "version": "1.0.0"
}
  • name: the package name, which must be unique if you publish it to NPM.
  • version: the project version, usually following Semantic Versioning (SemVer).

Semantic versions use the format major.minor.patch, such as 1.2.3:

  • Major: incompatible API changes.
  • Minor: backward-compatible feature additions.
  • Patch: backward-compatible bug fixes.

description and keywords

package.json
{
    "description": "A sample project for learning NPM",
    "keywords": ["npm", "tutorial", "frontend"]
}
  • description: a short description that helps others understand the project.
  • keywords: an array of keywords that helps people find the package after it is published to NPM.

main and type

package.json
{
    "main": "index.js",
    "type": "module"
}
  • main: the package entry point, loaded when someone calls require('your-package').
  • type: setting this to module makes ES modules the default, allowing import/export syntax; without it, CommonJS is the default.
type: "module" affects how Node.js interprets modules. Frontend projects usually rely on a bundler to handle module formats, so they may not need this field.

scripts

package.json
{
    "scripts": {
        "start": "node index.js",
        "dev": "node --watch index.js",
        "build": "webpack --mode=production"
    }
}

scripts defines commands you run with npm run script-name. For example:

npm run dev
npm run build
start and test are special scripts: run them with npm start and npm test, without run.

dependencies and devDependencies

These two fields are especially important, but beginners often confuse them.

package.json
{
    "dependencies": {
        "vue": "^3.5.0"
    },
    "devDependencies": {
        "eslint": "^9.22.0"
    }
}
FieldPurposeIncluded in production builds?
dependenciesRuntime packages, such as Vue, Axios, and dayjsYes
devDependenciesDevelopment tools, such as ESLint, Webpack, and TypeScriptNo
As a rule of thumb: put packages your website needs at runtime in dependencies, and tools used only during development in devDependencies.

Choose the field when installing a package:

npm install vue           # Added to dependencies by default
npm install eslint -D     # -D is short for --save-dev; adds to devDependencies

🪤 What happens if I put development dependencies in dependencies?

You might wonder why the distinction matters if both fields allow packages to be installed. Using the wrong field has several practical consequences:

1. Unnecessary packages are installed in production

Many deployment workflows run:

npm install --production

This command installs only dependencies, skipping devDependencies. If you put eslint, webpack, and typescript in dependencies, they are installed on the production server too.

In frontend projects, frameworks such as Vue and React become part of the build output, while ESLint, Prettier, and test tools do not. However, installing these tools on a server still uses disk space, slows installation, and increases security exposure.

2. Larger deployment packages

Suppose production needs only vue and axios, but you also put webpack, eslint, and sass in dependencies:

package.json
{
    "dependencies": {
        "vue": "^3.5.0",
        "axios": "^1.7.9",
        "webpack": "^5.97.0",
        "eslint": "^9.22.0",
        "sass": "^1.89.2"
    }
}

During deployment, the server downloads dozens or even hundreds of unnecessary packages, since each tool has its own dependency tree. This makes the resulting image larger and increases upload and startup times.

3. Greater exposure to vulnerabilities

Every additional package in production introduces potential security risks. Tools such as eslint and webpack have many dependencies; a vulnerability in any of them can affect your production security assessment.

4. Build output usually stays the same size, but dependency management gets harder

Modern frontend bundlers (Webpack, Vite, and Rollup) normally leave development tool code out of the final bundle. Putting a tool in the wrong field therefore does not usually make the JS/CSS files served to users larger.

However, node_modules becomes bloated, npm audit may report more vulnerabilities, and it becomes harder to identify which dependencies the project actually needs.


💡 A simple way to decide

Before installing a package, ask: “Will the application still need this package at runtime on the server?”

  • Yes → dependencies (for example, Vue, Axios, dayjs, and Express)
  • No → devDependencies (for example, ESLint, Prettier, Webpack, TypeScript, and test frameworks)
For a Node.js backend service, dependencies contains packages the running service actually uses. Debugging tools and test libraries used during development belong in devDependencies.

engines

package.json
{
    "engines": {
        "node": ">=18.0.0",
        "npm": ">=9.0.0"
    }
}

engines declares the Node.js and NPM versions required by the project. It is advisory by default and does not block installation, but you can enforce it through .npmrc.


🛠 A complete package.json example

package.json
{
    "name": "npm-study-demo",
    "version": "1.0.0",
    "description": "A sample project for learning how to use NPM",
    "main": "index.js",
    "type": "module",
    "scripts": {
        "start": "node index.js",
        "dev": "node --watch index.js",
        "lint": "eslint ."
    },
    "keywords": ["npm", "study"],
    "author": "Frontend Beginner",
    "license": "MIT",
    "dependencies": {
        "dayjs": "^1.11.13"
    },
    "devDependencies": {
        "eslint": "^9.22.0"
    },
    "engines": {
        "node": ">=18.0.0"
    }
}

❓ Questions and answers

Q1: How do dependencies and devDependencies differ?

dependencies contains packages needed at runtime, which may be included in a production build. devDependencies contains development tools whose own code is normally excluded from production output.

Q2: What if I put tools such as eslint and webpack in dependencies?

Production installs include unnecessary packages, deployment packages grow, and vulnerability exposure increases. The final frontend bundle usually stays the same size.

Q3: How do ^1.2.3 and ~1.2.3 differ?

^ allows updates that do not change the leftmost nonzero digit: ^1.2.3 allows compatible 1.x.x releases but not 2.0.0. ~ allows patch updates only: ~1.2.3 allows compatible 1.2.x releases.

Q4: What does -D mean in npm install eslint -D?

-D is short for --save-dev, which adds the package to devDependencies.

Q5: What does the main field do?

When someone loads your package, the module loader uses main to locate its entry point.


🧪 Practice

Open the npm-demo project from the previous article and make these changes:

  1. Add description and keywords to package.json.
  2. Add a script named hello that runs node -e "console.log('Hello NPM')".
  3. Install eslint as a development dependency:
# Enter your installation command here
  1. Check that eslint appears in the correct field in package.json.

🎉 You now understand the core fields in package.json! Next, we will explore installation commands and version ranges to make dependency management easier.