A package.json Template for a Library
Application manifests and library manifests look alike but serve different readers. An application's package.json is read by its own build; a library's is read by every package manager, bundler, type checker and security scanner that touches it. This template is a reasonable baseline for a TypeScript library that ships both ESM and CommonJS. Replace the placeholders and delete what you do not need.
The template
{
"name": "@scope/package-name",
"version": "0.1.0",
"description": "One sentence saying what the package does.",
"license": "MIT",
"author": "Your Name",
"repository": {
"type": "git",
"url": "git+https://example.invalid/owner/package-name.git"
},
"type": "module",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"require": "./dist/index.cjs"
},
"./package.json": "./package.json"
},
"main": "./dist/index.cjs",
"types": "./dist/index.d.ts",
"files": ["dist"],
"sideEffects": false,
"engines": { "node": ">=20" },
"scripts": {
"build": "your-build-command",
"test": "your-test-command",
"prepack": "npm run build"
},
"peerDependencies": {},
"dependencies": {},
"devDependencies": {}
}
The repository URL is a placeholder; point it at your real source so that provenance and "view source" links work.
Why each field is there
| Field | Reason |
|---|---|
license | An SPDX identifier that licence scanners read directly. |
type | Makes .js files ESM; the CommonJS build uses .cjs to stay unambiguous. |
exports | Defines the public entry points and blocks deep imports. types comes first because conditions match in order. |
"./package.json" export | Some tools read a dependency's manifest; exporting it avoids errors for them. |
main, types | Fallbacks for tools that predate exports. |
files | Allow-list of what ships; everything else stays out. |
sideEffects | Lets bundlers drop unused modules. Use an array if some files do have side effects. |
engines | States the oldest Node.js you test. |
prepack | Builds automatically before npm pack and npm publish, so stale output is not shipped. |
Simplifying it
- ESM only? Remove the
requirecondition andmain, and say so clearly in the README; older CommonJS consumers will need dynamicimport(). - No TypeScript? Drop
typesin both places, but consider shipping hand-written declarations anyway. - Unscoped name? Remove the scope, and check the name for confusing similarity to popular packages first; see typosquatting and package names.
- A plugin? Fill
peerDependencieswith the host's supported majors and keep it out ofdependencies.
A filled example (illustrative)
A hypothetical @example/slugify package would set "name": "@example/slugify", "description": "Turn titles into URL slugs.", keep the dual build, set engines.node to the oldest line in its CI matrix, and list nothing under dependencies. That last point is worth aiming for: each dependency you add becomes something your users also install and audit. Scoped packages publish as private by default on npm, so the first publish needs npm publish --access public; the scoped packages notes explain more.
Check the result with the npm pre-publish checklist and, for the exports block in detail, the exports field how-to.