What are module metadata fields in package.json?

JavaScript

The short answer

The package.json file has several fields that control how your module is loaded: main (CommonJS entry point), module (ES Module entry point), exports (modern, flexible entry points), and type (whether .js files are treated as CommonJS or ESM). These fields tell bundlers and Node.js which file to load when someone imports your package.

The key fields

main — the CommonJS entry point (used by Node.js and older bundlers):

{ "main": "dist/index.cjs.js" }

module — the ES Module entry point (used by bundlers like Webpack and Rollup for tree shaking):

{ "module": "dist/index.esm.js" }

exports — the modern way to define entry points (supports both CJS and ESM):

{
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
},
"./utils": {
"import": "./dist/utils.mjs",
"require": "./dist/utils.cjs"
}
}
}

type — tells Node.js how to treat .js files:

{ "type": "module" }

With "type": "module", .js files are treated as ES Modules. Without it (or with "type": "commonjs"), they are treated as CommonJS.

Other useful fields

  • files — which files to include when publishing to npm
  • sideEffects — tells bundlers if modules have side effects (enables better tree shaking)
  • browser — entry point for browser environments (different from Node.js)
{
"sideEffects": false,
"browser": "dist/index.browser.js"
}

Interview Tip

Know main, module, exports, and type. The most important one for modern projects is exports because it lets you define different entry points for different environments. Mentioning sideEffects: false for tree shaking shows you understand the build pipeline.

Why interviewers ask this

This tests your understanding of the JavaScript module ecosystem. Interviewers want to see if you know how packages are resolved and how to configure them for both CommonJS and ES Module consumers.