QA
Intermediate LevelNode JS

How do you publish dual CJS/ESM npm packages using package.json "exports" and conditional exports?

❓ Interview Prompt: Configure package.json: "exports": { ".": { "import": "./dist/index.mjs", "require": "./dist/index.cjs" } }. Node automatically routes import statements to ESM bundles and require() calls to CJS bundles.

Publishing dual-module packages requires configuring 'package.json' conditional exports:

•
'"import"': Path loaded when consumed via 'import' in ESM.
•
'"require"': Path loaded when consumed via 'require()' in CommonJS.

Code Implementation Example

json
{
  "name": "my-library",
  "exports": {
    ".": {
      "import": "./dist/index.mjs",
      "require": "./dist/index.cjs"
    }
  }
}
💡Configuring dual CJS/ESM package exports in package.json.
🎯 Interview Key Takeaway

Use package.json conditional exports to support both CommonJS and ES Module consumers.

#exports#ESM#CJS#npm Publishing#Intermediate
Back to all Node JS Questions→

More Node JS Interview Questions

Beginner LevelNode JS

How does the Node.js Event Loop work? Explain the 6 execution phases (Timers, Pending, Poll, Check, Close).

Beginner LevelNode JS

What does 'Single-Threaded, Event-Driven, Non-Blocking I/O' mean in Node.js?

Beginner LevelNode JS

What is the difference between process.nextTick() and setImmediate()?