{"record":{"id":"a69aba04e06fe318","repo":"vitejs/vite","slug":"failed-to-resolve-id-this-package-is-esm-only","errorCode":null,"errorMessage":"Failed to resolve ${id}. This package is ESM only but it was tried to load by `require`. See https://vite.dev/guide/troubleshooting.html#this-package-is-esm-only for more details.","messagePattern":"Failed to resolve (.+?)\\. This package is ESM only but it was tried to load by `require`\\. See https://vite\\.dev/guide/troubleshooting\\.html#this-package-is-esm-only for more details\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vite/src/node/config.ts","lineNumber":2585,"sourceCode":"            }\n\n            const isImport = isESM || kind === 'dynamic-import'\n            let idFsPath: string | undefined\n            try {\n              idFsPath = nodeResolveWithVite(id, importer, {\n                root,\n                isRequire: !isImport,\n              })\n            } catch (e) {\n              if (!isImport) {\n                let canResolveWithImport = false\n                try {\n                  canResolveWithImport = !!nodeResolveWithVite(id, importer, {\n                    root,\n                  })\n                } catch {}\n                if (canResolveWithImport) {\n                  throw new Error(\n                    `Failed to resolve ${JSON.stringify(\n                      id,\n                    )}. This package is ESM only but it was tried to load by \\`require\\`. See https://vite.dev/guide/troubleshooting.html#this-package-is-esm-only for more details.`,\n                  )\n                }\n              }\n              throw e\n            }\n            if (!idFsPath) return\n            // always no-externalize json files as rolldown does not support import attributes\n            if (idFsPath.endsWith('.json')) {\n              return idFsPath\n            }\n\n            if (idFsPath && isImport) {\n              idFsPath = pathToFileURL(idFsPath).href\n            }\n            return { id: idFsPath, external: true }","sourceCodeStart":2567,"sourceCodeEnd":2603,"githubUrl":"https://github.com/vitejs/vite/blob/b4d66fee14d970f45b8a6f3d7d6aee73ca9b88ab/packages/vite/src/node/config.ts#L2567-L2603","documentation":"During config bundling Vite resolves import specifiers with nodeResolveWithVite. When a specifier resolves as ESM-only (require fails) but works as a static import, Vite throws a targeted error pointing at the troubleshooting guide. This typically means an ESM-only package is being pulled into a CJS context, or a CJS-resolver path was forced.","triggerScenarios":"An ESM-only dependency (no CJS entry) is required via require() in a config or plugin loaded in CJS mode; or the resolver falls back to the require path and the package lacks main/require fields.","commonSituations":"Mixing ESM-only packages (e.g. 'chalk', 'execa' v6+) into a CommonJS vite.config.js; older Node versions whose default resolution prefers require; packages whose package.json lacks a valid exports map.","solutions":["Rename your config to vite.config.mjs (or set package.json type: module) so it is loaded as ESM.","Downgrade or replace the ESM-only package with a CJS-compatible version.","Bundle the dependency into the config (configLoader: 'bundle') rather than requiring it at runtime."],"exampleFix":"// before: vite.config.js (CommonJS) requires an ESM-only package\nconst pkg = require('esm-only-pkg')\n// after: rename to vite.config.mjs and import\nimport pkg from 'esm-only-pkg'","handlingStrategy":"validation","validationCode":"function loadConfigAsEsm(specifier) {\n  try { return require.resolve(specifier); }\n  catch (e) {\n    if (/^Error\\(ERR_REQUIRE_ESM\\)/.test(String(e.message)) || e.code === 'ERR_REQUIRE_ESM') {\n      throw new Error(`${specifier} is ESM-only; load the config as ESM (vite.config.mjs)`);\n    }\n    throw e;\n  }\n}","typeGuard":"function looksEsmOnly(pkg) {\n  return Boolean(pkg && pkg.type === 'module' && !pkg.main);\n}","tryCatchPattern":null,"preventionTips":["Use vite.config.mjs when depending on ESM-only packages.","Set package.json type: module for ESM projects."],"tags":["config","esm","commonjs","resolve"],"backgroundTag":null,"analyzedSha":"b4d66fee14d970f45b8a6f3d7d6aee73ca9b88ab","analyzedAt":"2026-08-11T11:49:19.515Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}