Skip to content

Commit

Permalink
fix(config): remove error if require resolve to esm (#18437)
Browse files Browse the repository at this point in the history
Co-authored-by: 翠 / green <[email protected]>
  • Loading branch information
bluwy and sapphi-red authored Oct 28, 2024
1 parent 5a967cb commit f886f75
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 14 deletions.
7 changes: 4 additions & 3 deletions docs/guide/troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`](<https://nodejs.org/docs/latest-v18.x/api/esm.html#require:~:text=Using%20require%20to%20load%20an%20ES%20module%20is%20not%20supported%20because%20ES%20modules%20have%20asynchronous%20execution.%20Instead%2C%20use%20import()%20to%20load%20an%20ES%20module%20from%20a%20CommonJS%20module.>).
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`
Expand Down
11 changes: 0 additions & 11 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit f886f75

Please sign in to comment.