Full answer
package.json is the manifest of a JavaScript project. Important fields:
nameandversion: identify the package (required for publishing).type:"module"makes.jsfiles ES modules; the default is CommonJS.main,module, andexports: entry points.exportsis the modern field, and it also limits which files outsiders can import.scripts: named commands run withnpm run name.npm testandnpm starthave shortcuts.npxruns a package binary without a global install.dependencies: packages needed when the code runs.devDependencies: tools needed only to build and test, such as bundlers and test runners.peerDependencies: packages the consumer must provide, common for plugins and component libraries.optionalDependenciesare allowed to fail on install.engines: the Node.js versions the package supports.filesand.npmignorecontrol what is published.
Semantic versioning has three numbers: major.minor.patch. Patch is for bug fixes, minor for backward compatible features, major for breaking changes. Ranges: ^1.2.3 accepts >=1.2.3 <2.0.0 (minor and patch updates), ~1.2.3 accepts >=1.2.3 <1.3.0 (patch updates only), and an exact 1.2.3 accepts only that. For 0.x versions the caret is stricter: ^0.2.3 means >=0.2.3 <0.3.0, because 0.x is treated as unstable.
The lockfile (package-lock.json) records the exact version of every installed package, including indirect ones. Commit it. npm install can update it within the ranges. npm ci installs exactly what the lockfile says, deletes node_modules first, and fails on a mismatch, so use it in continuous integration. Do not commit node_modules.
Security and hygiene: run npm audit, keep dependencies small, check packages before installing them, and know that install scripts run code on your machine (a supply chain risk).
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const { execFileSync } = require('node:child_process');
// a minimal range checker for ^, ~ and exact versions
const parse = (v) => v.split('.').map(Number);
function satisfies(version, range) {
const v = parse(version);
const operator = /^[\^~]/.test(range) ? range[0] : '=';
const base = parse(range.replace(/^[\^~]/, ''));
const atLeast = v[0] > base[0] || (v[0] === base[0] && (v[1] > base[1] || (v[1] === base[1] && v[2] >= base[2])));
if (!atLeast) return false;
if (operator === '=') return version === range;
if (operator === '~') return v[0] === base[0] && v[1] === base[1];
if (base[0] > 0) return v[0] === base[0];
return v[0] === 0 && v[1] === base[1];
}
for (const [version, range] of [['1.9.5', '^1.2.3'], ['2.0.0', '^1.2.3'], ['1.2.9', '~1.2.3'], ['1.3.0', '~1.2.3'], ['0.2.9', '^0.2.3'], ['0.3.0', '^0.2.3'], ['1.2.2', '^1.2.3']]) {
console.log(range.padEnd(7), version.padEnd(6), satisfies(version, range));
}
// the type field decides how a .js file is loaded
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'pkg-'));
fs.writeFileSync(path.join(dir, 'app.js'), "import { sep } from 'node:path';\nconsole.log('loaded as an ES module', typeof sep);\n");
const run = () => {
try {
return execFileSync(process.execPath, [path.join(dir, 'app.js')], { cwd: dir, stdio: ['ignore', 'pipe', 'pipe'] }).toString().trim();
} catch (error) {
return 'failed: ' + (/SyntaxError/.test(error.stderr.toString()) ? 'SyntaxError (import in a CommonJS file)' : 'other error');
}
};
fs.writeFileSync(path.join(dir, 'package.json'), JSON.stringify({ name: 'demo', version: '1.0.0', type: 'module' }));
console.log('type module ->', run());
fs.writeFileSync(path.join(dir, 'package.json'), JSON.stringify({ name: 'demo', version: '1.0.0', type: 'commonjs' }));
console.log('type commonjs ->', run());
fs.rmSync(dir, { recursive: true, force: true });
console.log(JSON.stringify({ scripts: { test: 'node --test', start: 'node server.js' }, engines: { node: '>=20' } }));Run with Node.js v24.22.1 as js-129.js; two runs gave identical output. Output: ^1.2.3 1.9.5 true ^1.2.3 2.0.0 false ~1.2.3 1.2.9 true ~1.2.3 1.3.0 false ^0.2.3 0.2.9 true ^0.2.3 0.3.0 false ^1.2.3 1.2.2 false type module -> loaded as an ES module string type commonjs -> failed: SyntaxError (import in a CommonJS file) {"scripts":{"test":"node --test","start":"node server.js"},"engines":{"node":">=20"}}
Key points
- Key fields: name, version, type, exports, scripts, dependencies, devDependencies, engines.
- dependencies are for runtime, devDependencies for tooling, peerDependencies must be supplied by the consumer.
- Caret allows minor and patch updates, tilde only patch; the caret is stricter for 0.x versions.
- Commit the lockfile; use npm ci in continuous integration for repeatable installs.
- Audit dependencies; install scripts and packages are a supply chain risk.
Common mistakes
- Putting build tools in dependencies, or runtime packages in devDependencies, so production installs break.
- Using npm install in continuous integration and getting different versions from the lockfile.
- Not committing the lockfile.
- Using broad ranges such as star or latest for production dependencies.
Follow-ups
- What is the difference between npm install and npm ci?
- What is a peer dependency and why do plugins use it?
- How would you protect a project from a malicious package?
Tips
- Explain caret and tilde with one example each.
- Say what the lockfile is for and when to use npm ci.
- Separate runtime from development dependencies.