{"record":{"id":"2fc304de6bafbcc3","repo":"parcel-bundler/parcel","slug":"output-format-esmodule-cannot-be-used-in-the-ma","errorCode":null,"errorMessage":"Output format \"esmodule\" cannot be used in the \"main\" target without a .mjs extension or \"type\": \"module\" field.","messagePattern":"Output format \"esmodule\" cannot be used in the \"main\" target without a \\.mjs extension or \"type\": \"module\" field\\.","errorType":"exception","errorClass":"ThrowableDiagnostic","httpStatus":null,"severity":"error","filePath":"packages/core/core/src/requests/TargetRequest.js","lineNumber":827,"sourceCode":"\n        let outputFormat =\n          descriptor.outputFormat ??\n          this.options.defaultTargetOptions.outputFormat ??\n          inferredOutputFormat ??\n          (targetName === 'module' ? 'esmodule' : 'commonjs');\n        let isModule = outputFormat === 'esmodule';\n\n        if (\n          targetName === 'main' &&\n          outputFormat === 'esmodule' &&\n          inferredOutputFormat !== 'esmodule'\n        ) {\n          let contents: string =\n            typeof pkgContents === 'string'\n              ? pkgContents\n              : // $FlowFixMe\n                JSON.stringify(pkgContents, null, '\\t');\n          throw new ThrowableDiagnostic({\n            diagnostic: {\n              // prettier-ignore\n              message: md`Output format \"esmodule\" cannot be used in the \"main\" target without a .mjs extension or \"type\": \"module\" field.`,\n              origin: '@parcel/core',\n              codeFrames: [\n                {\n                  language: 'json',\n                  filePath: pkgFilePath ?? undefined,\n                  code: contents,\n                  codeHighlights: generateJSONCodeHighlights(contents, [\n                    {\n                      key: `/targets/${targetName}/outputFormat`,\n                      type: 'value',\n                      message: 'Declared output format defined here',\n                    },\n                    {\n                      key: '/main',\n                      type: 'value',","sourceCodeStart":809,"sourceCodeEnd":845,"githubUrl":"https://github.com/parcel-bundler/parcel/blob/59484858a1a0bcbb71f74088956bb437a2db6505/packages/core/core/src/requests/TargetRequest.js#L809-L845","documentation":"The `main` target conventionally emits CommonJS for Node. If you set its outputFormat to `esmodule` but the distEntry does not use `.mjs` AND package.json has no `\"type\": \"module\"`, Node would load the output as CommonJS, breaking ESM semantics. Parcel refuses to silently produce a mis-loaded bundle. The guard fires when targetName==='main', outputFormat==='esmodule', and the inferredOutputFormat (from extension/package type) is not esmodule.","triggerScenarios":"Configuring `targets.main.outputFormat = \"esmodule\"` (or relying on main with .js) while package.json lacks `\"type\": \"module\"` and the main distEntry is not `.mjs`.","commonSituations":"Switching a CommonJS library to ESM output without updating package.json type field or extension; using `main` instead of `module` for ESM; partial ESM migration where the runtime loader would misinterpret the file.","solutions":["Add `\"type\": \"module\"` to package.json so Node treats .js as ESM.","Change the `main` distEntry to a `.mjs` extension.","Move ESM output to the `module` target instead of `main`.","Switch the outputFormat back to `commonjs` if ESM is not required."],"exampleFix":"// before (package.json)\n{\n  \"main\": \"dist/index.js\",\n  \"targets\": { \"main\": { \"outputFormat\": \"esmodule\" } }\n}\n\n// after\n{\n  \"type\": \"module\",\n  \"main\": \"dist/index.js\",\n  \"targets\": { \"main\": { \"outputFormat\": \"esmodule\" } }\n}","handlingStrategy":"validation","validationCode":"function assertMainEsmCompat(pkg) {\n  const isMainEsm = pkg.targets?.main?.outputFormat === 'esmodule'\n    || (pkg.main && pkg.main.endsWith('.mjs'));\n  if (isMainEsm && pkg.type !== 'module' && !pkg.main?.endsWith('.mjs')) {\n    throw new Error('Add \"type\":\"module\" or use .mjs for the main target ESM output.');\n  }\n}","typeGuard":"function mainEsmIsWellFormed(pkg) {\n  const of = pkg.targets?.main?.outputFormat;\n  if (of !== 'esmodule') return true;\n  return pkg.type === 'module' || (typeof pkg.main === 'string' && pkg.main.endsWith('.mjs'));\n}","tryCatchPattern":"try { await parcel.run(); } catch (e) {\n  if (/cannot be used in the \"main\" target/.test(e.message)) {\n    console.error('Add \"type\":\"module\" to package.json or rename main output to .mjs.');\n  } else throw e;\n}","preventionTips":["When emitting ESM from `main`, set `\"type\":\"module\"` or use `.mjs`.","Prefer the `module` target for ESM output.","Audit package.json `type`/extension alignment during ESM migrations."],"tags":["parcel","esm","commonjs","main-target","node","config"],"backgroundTag":null,"analyzedSha":"59484858a1a0bcbb71f74088956bb437a2db6505","analyzedAt":"2026-08-13T04:06:35.925Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}