Full answer
Important fields:
- name and version: the identity of the package. A published name must be unique on the registry.
- type: "module" or "commonjs". Decides how .js files are loaded.
- main: the entry file for require(). exports: a modern map of entry points that also blocks access to other files.
- scripts: named commands run with npm run <name>. start and test have shortcuts (npm start, npm test). npm also runs pre and post hooks, for example prebuild before build.
- dependencies: packages needed at run time. They are installed in production.
- devDependencies: packages needed only for development, such as test runners and linters.
- peerDependencies: packages the host project must provide, like a plugin that expects react. They state a compatible range and are not installed by default for the library itself.
- optionalDependencies: install may fail without breaking the install.
- engines: the Node versions the project supports. npm warns, and with engine-strict it fails.
- files and .npmignore: what is included when you publish.
- private: true: blocks accidental publishing.
- bin: maps command names to files, for command-line tools.
npm install adds to dependencies by default and --save-dev adds to devDependencies. In production, npm ci --omit=dev installs only what the app needs at run time.
javascript
const sample = `{
"name": "attendance-tracker",
"version": "1.4.2",
"private": true,
"type": "module",
"main": "./src/index.js",
"scripts": {
"start": "node src/index.js",
"test": "node --test",
"prebuild": "echo cleaning",
"build": "echo building"
},
"dependencies": { "express": "^4.19.2" },
"devDependencies": { "eslint": "^9.0.0" },
"peerDependencies": { "react": ">=18" },
"engines": { "node": ">=20" }
}`;
const pkg = JSON.parse(sample);
console.log('package:', pkg.name + '@' + pkg.version, '| module type:', pkg.type);
console.log('scripts you can run:', Object.keys(pkg.scripts).join(', '));
console.log('needed in production:', Object.keys(pkg.dependencies));
console.log('only for development:', Object.keys(pkg.devDependencies));
console.log('host must provide:', pkg.peerDependencies);
console.log('minimum Node version rule:', pkg.engines.node);The JSON is a sample. The snippet shows how tools read the fields. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): package: attendance-tracker@1.4.2 | module type: module scripts you can run: start, test, prebuild, build needed in production: [ 'express' ] only for development: [ 'eslint' ] host must provide: { react: '>=18' } minimum Node version rule: >=20
Key points
- scripts, dependencies and type are used every day.
- dependencies are needed at run time. devDependencies are only for development.
- peerDependencies tell the host project which versions to supply.
- exports controls the public entry points of a package.
Common mistakes
- Putting build tools in dependencies of an application.
- Forgetting that private: true prevents publishing.
- Using main and exports together and not testing both.
- Believing engines blocks installs by default. It only warns unless engine-strict is set.
Follow-ups
- What is the difference between npm run and npx?
- What do pre and post scripts do?
- How do you publish only the dist folder?
Tips
- Group the fields by purpose. Do not list them in random order.
- Explain peerDependencies with a plugin example.
- Mention exports, as it is the modern entry point.