Interview Q&A / Node.js

BeginnerQuick answer1 min readUpdated 3 Oct 2026

What does the main field in package.json do?

SHORT ANSWER

main names the entry file that Node loads when someone requires or imports your package by name. If it is missing, Node falls back to index.js. Modern packages may also use the exports field.

Explain it simply

When you write require("fest-utils"), Node finds the folder node_modules/fest-utils and then asks, "Which file is the front door?" The main field is the answer. It is like the signboard at a college gate that tells visitors which building to enter first.

Example

json
{
  "name": "fest-utils",
  "version": "2.0.0",
  "main": "lib/index.js"
}

With this, require("fest-utils") loads lib/index.js. If main is absent, Node tries index.js at the package root.

Two related points:

  • For an application you run yourself, main matters little. You usually start it with node src/server.js or an npm script.
  • The newer exports field gives more control: it can point to different files for require and import, and it blocks deep imports like fest-utils/lib/secret.js that you did not intend to publish. When exports exists, it takes priority over main.
json
{
  "exports": {
    ".": { "import": "./esm/index.js", "require": "./cjs/index.cjs" }
  }
}

What to say in the interview

"main is the entry point used when the package is required by name, defaulting to index.js. exports is the modern replacement that supports separate ESM and CJS entries and hides internal files."

Common mistakes

  • Pointing main at a file that is not included in the published package.
  • Forgetting that exports overrides main.

#npm#package-json#modules

Related questions

Practise more Node.js questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Node.js quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.