Developer and Text Tools

How to Write and Check a package.json Before You Publish

The package.json fields that matter, the exports map rules that break imports, and a quick way to build or validate the file before npm publish.

Saizul Amin
Saizul Amin5 Nov 2026 · 4 min read
How to Write and Check a package.json Before You Publish

A package.json file is the contract between your code and everything that installs it. npm reads it to publish and install, Node.js reads it to resolve imports, TypeScript reads it to find types, and bundlers read it to decide which file to include. Most of the time it works quietly. When it does not, the symptoms are confusing: a package that installs but cannot be imported, types that are not found, a command-line tool that does nothing, or a publish that npm rejects.

The fields that matter most

name and version. Names must be lowercase, URL-safe, at most 214 characters, must not start with a dot or underscore, and must not be the name of a Node.js core module such as fs or http. Scoped names look like @scope/name. The version must follow semantic versioning: three numbers, optionally followed by a pre-release tag such as -beta.1.

license. Use an SPDX identifier such as MIT, ISC or Apache-2.0. Old ids such as GPL-3.0 are deprecated in favour of GPL-3.0-only or GPL-3.0-or-later.

type, main and types. type set to module makes .js files ES modules; without it they are CommonJS. main is the entry for older tools and types points TypeScript to your declaration file.

exports. This field is where most modern packaging mistakes happen, so it gets its own section below.

files. An allow-list of what gets published. Without it, tests, fixtures and source maps often end up on npm.

engines and packageManager. engines.node tells users which Node versions you support. packageManager pins the exact package manager version for Corepack, written as [email protected] or similar.

The exports map in practice

Once exports exists, Node.js only lets people import the paths listed in it. Each entry can be a single path or an object of conditions such as types, import, require and default. Conditions are checked from top to bottom and the first match wins. That gives three simple rules: every target must start with ./, types must come first so TypeScript sees it, and default must come last because nothing after it is ever reached. Also, never mix subpaths that start with a dot and conditions in the same object.

Building a file from scratch

The package.json Generator and Validator has a form for every field above. Fill in the basics, paste a repository address and it is rewritten in the git+https form npm prefers, then choose the module type and apply an exports preset: dual ESM and CommonJS, ESM only, CommonJS only, or a subpath pattern. Script presets cover a TypeScript library, a Node CLI, a Vite app and a Next.js app, and you can add bin commands for a CLI.

Dependencies go in four groups. The quickest way to fill them is the bulk box: paste lines like react@^18.3.1 or vitest and choose the group. Bare names get latest, which the checks flag so you remember to pin a range. The output is written in the conventional field order with dependencies sorted alphabetically, ready to copy or download.

Checking an existing file

Switch to Check an existing file and paste or open your package.json. The checks fall into three levels. Errors are things npm, Node.js or TypeScript will act on, such as an invalid name or version, an engines value that is not a semver range, or an exports target without ./. Warnings are legal but risky: a dependency on *, a local file: path in a package you plan to publish, the same package in both dependencies and devDependencies, or an install script that will run on every user's machine. Tips point out conventions, such as moving typescript and eslint to devDependencies or adding a files list.

If there is a lot to fix, press Load into the form. The known fields move into the builder, where undo and redo cover every edit. Fields the form does not handle, such as workspaces or browserslist, are listed so you can add them back by hand.

What the tool cannot tell you

It does not contact the npm registry, so it cannot say whether a version exists or whether a name is already taken. Before a first publish, run npm publish with the dry-run flag to see the exact files that will be included, and check the name on npmjs.com. Used together, those two steps and a clean check catch nearly every packaging problem before your users do.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages