Build a valid package.json without guessing field names
Every Node.js project is anchored by a package.json — the manifest that
declares its name, version, dependencies, and the scripts you run with npm run.
A typo in a field name or an invalid version range silently breaks tooling. This
builder collects the standard fields, validates the ones with strict rules (name
casing, semver versions), and emits clean, indented JSON ready to drop into your
project root.
How it works
The builder assembles a single JSON object from your inputs, only including fields you actually fill in so the output stays minimal. A few fields have rules the tool enforces:
- name is lowercased and stripped of illegal characters; scoped names like
@acme/widgetare preserved. - version follows semantic versioning,
MAJOR.MINOR.PATCH, defaulting to1.0.0. - scripts, dependencies, and devDependencies become nested objects, for example:
{
"scripts": { "build": "tsc", "test": "jest" },
"dependencies": { "express": "^4.19.0" }
}
Dependency versions use npm range syntax: ^1.2.3 allows compatible minor and
patch updates, ~1.2.3 allows only patch updates, and an exact 1.2.3 pins the
version. The output is serialized with two-space indentation, the convention npm
itself writes.
Tips and notes
- Set
private: trueon applications so a straynpm publishcannot leak them to the public registry. - Keep your entry point (
main) accurate; bundlers andrequire()resolution rely on it for packages consumed by others. - Use
enginesto document the Node version your CI runs, so contributors get a warning instead of cryptic runtime errors. - After saving, run
npm installto generate the lockfile that pins the exact resolved versions of every dependency.