diff --git a/docs/guide/troubleshooting.md b/docs/guide/troubleshooting.md index 7386a7cead4b21..1804b589ceb3ed 100644 --- a/docs/guide/troubleshooting.md +++ b/docs/guide/troubleshooting.md @@ -54,11 +54,12 @@ When importing a ESM only package by `require`, the following error happens. > Failed to resolve "foo". This package is ESM only but it was tried to load by `require`. -> "foo" resolved to an ESM file. ESM file cannot be loaded by `require`. +> Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/dependency.js from /path/to/vite.config.js not supported. +> Instead change the require of index.js in /path/to/vite.config.js to a dynamic import() which is available in all CommonJS modules. -ESM files cannot be loaded by [`require`](). +In Node.js <=22, ESM files cannot be loaded by [`require`](https://nodejs.org/docs/latest-v22.x/api/esm.html#require) by default. -We recommend converting your config to ESM by either: +While it may work using [`--experimental-require-module`](https://nodejs.org/docs/latest-v22.x/api/modules.html#loading-ecmascript-modules-using-require), or Node.js >22, or in other runtimes, we still recommend converting your config to ESM by either: - adding `"type": "module"` to the nearest `package.json` - renaming `vite.config.js`/`vite.config.ts` to `vite.config.mjs`/`vite.config.mts` diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index faad10167c27aa..95872da6492e14 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -1637,17 +1637,6 @@ async function bundleConfigFile( if (idFsPath && isImport) { idFsPath = pathToFileURL(idFsPath).href } - if ( - idFsPath && - !isImport && - isFilePathESM(idFsPath, packageCache) - ) { - throw new Error( - `${JSON.stringify( - id, - )} resolved to an ESM file. ESM file cannot be loaded by \`require\`. See https://vite.dev/guide/troubleshooting.html#this-package-is-esm-only for more details.`, - ) - } return { path: idFsPath, external: true,