How Well Do You Know package.json? A Nine-Question Quiz

Most of a JavaScript package's behaviour on install and import is decided by a handful of fields in one JSON file. These questions focus on the fields that cause real bugs when they are wrong. Answers and explanations sit right below each question, so decide before you read on. The npm Docs and the Node.js documentation on packages are the reference for everything here.

Entry points

1. A package has both main and exports. Which does a modern Node.js use for import "package-a"?

Answer: exports. When exports is defined, Node.js uses it and ignores main for resolution. main is still worth keeping for older tools.

2. After adding an exports map, a user's require("package-a/lib/helpers") breaks. Why?

Answer: exports encapsulates the package. Only listed subpaths are reachable. Unlisted files produce ERR_PACKAGE_PATH_NOT_EXPORTED, which is why adding exports to an existing package is often a breaking change.

3. Inside an exports condition object, does order matter?

Answer: yes. Conditions are matched in the order they are written and the first match wins. Put types first and default last.

"exports": {
  ".": {
    "types": "./dist/index.d.ts",
    "import": "./dist/index.js",
    "require": "./dist/index.cjs",
    "default": "./dist/index.js"
  }
}

What gets published

4. You set "files": ["dist"]. Will your README be published?

Answer: yes. npm always includes certain files, including package.json, the README and the licence file, whatever files says.

5. Is "private": true a way to keep some files out of the tarball?

Answer: no. It makes npm refuse to publish the package at all. It is used for application roots and monorepo workspace roots that should never reach the registry.

Dependencies and environment

6. Your plugin needs the host framework at runtime but should share the user's copy. Which field?

Answer: peerDependencies. Putting the framework in dependencies can install a second copy, which breaks anything that relies on a single instance. Optionally add peerDependenciesMeta to mark a peer as optional.

7. You set "engines": { "node": ">=20" }. Will npm refuse to install on Node.js 18?

Answer: by default it warns rather than failing. npm only enforces it when the user enables engine-strict. The field still documents your support promise and some other package managers treat it more strictly.

Build tools and binaries

8. What does "sideEffects": false tell a bundler?

Answer: that importing a module for its side effects alone does nothing, so unused modules can be dropped. It is a hint for tree-shaking in bundlers such as webpack. If some files do have side effects (polyfills, CSS imports), list them in an array instead of using false.

9. A bin entry points at ./cli.js, but running the command fails on macOS and Linux with an odd syntax error. What is likely missing?

Answer: the shebang line. The file needs #!/usr/bin/env node as its first line so the shell runs it with Node.js. npm creates the command link on install, but the script must declare its interpreter.

Your result

Seven or more correct means the fields hold few surprises for you; check your own package with the npm pre-publish checklist. Four to six: the exports field how-to and the sideEffects Q&A fill the common gaps. Fewer than four: start with the peer dependencies guide and the post on the engines field, then come back and try again.

Related pages