How to use the package.json generator
- Enter the Name, Version and Description.
- Choose the Module type: ES modules or CommonJS. Set Licence (SPDX), Author and Repository (owner/name or URL).
- Add Keywords (comma-separated) and the Entry point, such as
dist/index.js. - List Scripts (name: command), one per line, then Dependencies (name@version) and Dev dependencies (name@version).
- Set the Node.js engines range, tick Private (never published) if needed, and copy the JSON into
package.json.
What it does and when to use it
Every Node.js project and npm package has a package.json. It names the project, lists the packages it needs and defines commands such as npm test. By hand it is easy to slip: a missing comma, an upper-case name, or a repository URL npm does not recognise.
Use it to start a project or tidy an old file. The scripts you define are the ones the GitHub Actions generator calls. Put the same SPDX id in the licence generator, and add the matching compiler settings with the tsconfig generator. To check a file you already have, paste it into the JSON validator.
How it works
The tool builds a JSON object in a fixed field order and prints it with two-space indentation:
- Name check: the npm rules for new packages, at most 214 characters, lower-case, URL-safe characters only, no leading dot or underscore. Problems appear as a warning under the file.
- Repository:
owner/namebecomesgit+https://github.com/owner/name.git; a full https URL gets thegit+prefix and.gitending. - Entry point: sets both
mainand anexportsentry for".". - Dependencies: each line can be
name@^1.2.3,@scope/name@1.0.0,name: ^1.2.3orname 1.2.3. Lists are sorted, and a package in both lists is flagged.
The npm documentation for package.json describes every field.
Worked examples
The default library. With the starting values, the repository becomes:
"repository": {
"type": "git",
"url": "git+https://github.com/asharao/invoice-parser.git"
}
and "main": "dist/index.js" is paired with "exports": { ".": "./dist/index.js" }.
An invalid name. Name My Package still produces a file, with two warnings: “New package names must be lower-case.” and a note that names may contain only URL-safe characters.
A private scoped CLI. Name @northwind/cli-tools, CommonJS, no entry point, Private ticked, repository https://gitlab.com/northwind/cli-tools, dependencies chalk 5.4.1 and commander: ^14.0.0, and commander again as a dev dependency. The URL becomes git+https://gitlab.com/northwind/cli-tools.git, and a warning says commander is listed in both lists.
Limits and tips
- Review exports. With an exports map, other code can import only the paths listed there.
- Fields not in the form, such as bin, files or workspaces, must be added by hand.
- Never put tokens in scripts. Use environment variables instead.
Frequently asked questions
Is this the same as running npm init?
Which version numbers should I write for dependencies?
What does Private do?
ES modules or CommonJS?
Why are my dependencies in a different order?
How do we know nothing is uploaded? Test it yourself on the privacy proof page.