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→