-
-
Notifications
You must be signed in to change notification settings - Fork 8k
Vite doesn't support ESM packages with "exports" field in SSR #3953
Description
Describe the bug
Because of this browserify/resolve#222 issue, Vite won't parse package.json "exports" field and will throw this error for packages that doesn't use "main" field:
3:03:07 AM [vite] Error when evaluating SSR module /src/components/context.ts:
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\workspace\m7d\svelte-context-enhanced\website\node_modules\@raythurnevoid\svelte-context-enhanced\index.js
require() of ES modules is not supported.
require() of C:\workspace\m7d\svelte-context-enhanced\website\node_modules\@raythurnevoid\svelte-context-enhanced\index.js from C:\workspace\m7d\vite\packages\vite\dist\node\chunks\dep-4058344d.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.I tried to replace resolve packages with webpack's enhanced-resolve: https://www.npmjs.com/package/enhanced-resolve
Their base code is extremely more hure and complicated than resolve but i've managed to make it work well in Vite: https://github.com/raythurnevoid/vite/commit/95a4f859fa31c4f052f2c3ab422b8f6e82154269
Another upside of enhanced-resolve is that it already supports also "imports" field, that could be a good replace for the numerous "alias" fields of tools like Jest, Rollup, Vite itself and tsconfig paths:
https://nodejs.org/api/packages.html#packages_imports
https://nodejs.org/api/packages.html#packages_subpath_imports
if this fix is feasible i'll open a PR with it.
Reproduction
To reproduce it's enough to import an ESM only package eg: https://github.com/raythurnevoid/strings-filter
Repo example: https://github.com/raythurnevoid/vite-esm-error-reproduction/tree/main
System Info
Output of npx envinfo --system --npmPackages vite,@vitejs/plugin-vue --binaries --browsers:
System:
OS: Windows 10 10.0.19042
CPU: (16) x64 AMD Ryzen 9 4900H with Radeon Graphics
Memory: 16.09 GB / 31.42 GB
Binaries:
Node: 16.2.0 - C:\Program Files\nodejs\node.EXE
Yarn: 1.22.10 - ~\AppData\Roaming\npm\yarn.CMD
npm: 7.13.0 - C:\Program Files\nodejs\npm.CMD
Browsers:
Edge: Spartan (44.19041.1023.0), Chromium (91.0.864.54)
Internet Explorer: 11.0.19041.1Used package manager: npm
Logs
shows logs
PS C:\workspace\m7d\vite-esm> npm run dev
> test-ssr-vue@0.0.0 dev
> node server
http://localhost:3000
[@vue/compiler-sfc] <script setup> is still an experimental proposal.
Follow its status at https://github.com/vuejs/rfcs/pull/227.
[@vue/compiler-sfc] When using experimental features,
it is recommended to pin your vue dependencies to exact versions to avoid breakage.
7:35:45 PM [vite] Error when evaluating SSR module /src/pages/Home.vue:
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\workspace\m7d\vite-esm\node_modules\@raythurnevoid\strings-filter\index.js
require() of ES modules is not supported.
require() of C:\workspace\m7d\vite-esm\node_modules\@raythurnevoid\strings-filter\index.js from C:\workspace\m7d\vite-esm\node_modules\vite\dist\node\chunks\dep-0ed4fbc0.js is an ES module file as it is a .js file whose nearest parent package.json contains
"type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\workspace\m7d\vite-esm\node_modules\@raythurnevoid\strings-filter\package.json.
at new NodeError (node:internal/errors:363:5)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1126:13)
at Module.load (node:internal/modules/cjs/loader:989:32)
at Function.Module._load (node:internal/modules/cjs/loader:829:14)
at Module.require (node:internal/modules/cjs/loader:1013:19)
at require (node:internal/modules/cjs/helpers:93:18)
at nodeRequire (C:\workspace\m7d\vite-esm\node_modules\vite\dist\node\chunks\dep-0ed4fbc0.js:69996:17)
at ssrImport (C:\workspace\m7d\vite-esm\node_modules\vite\dist\node\chunks\dep-0ed4fbc0.js:69949:20)
at eval (/src/pages/Home.vue:7:31)
at instantiateModule (C:\workspace\m7d\vite-esm\node_modules\vite\dist\node\chunks\dep-0ed4fbc0.js:69982:166)
[Vue Router warn]: uncaught error during route navigation:
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\workspace\m7d\vite-esm\node_modules\@raythurnevoid\strings-filter\index.js
require() of ES modules is not supported.
require() of C:\workspace\m7d\vite-esm\node_modules\@raythurnevoid\strings-filter\index.js from C:\workspace\m7d\vite-esm\node_modules\vite\dist\node\chunks\dep-0ed4fbc0.js is an ES module file as it is a .js file whose nearest parent package.json contains
"type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\workspace\m7d\vite-esm\node_modules\@raythurnevoid\strings-filter\package.json.
at new NodeError (node:internal/errors:363:5)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1126:13)
at Module.load (node:internal/modules/cjs/loader:989:32)
at Function.Module._load (node:internal/modules/cjs/loader:829:14)
at Module.require (node:internal/modules/cjs/loader:1013:19)
at require (node:internal/modules/cjs/helpers:93:18)
at nodeRequire (C:\workspace\m7d\vite-esm\node_modules\vite\dist\node\chunks\dep-0ed4fbc0.js:69996:17)
at ssrImport (C:\workspace\m7d\vite-esm\node_modules\vite\dist\node\chunks\dep-0ed4fbc0.js:69949:20)
at eval (/src/pages/Home.vue:7:31)
at instantiateModule (C:\workspace\m7d\vite-esm\node_modules\vite\dist\node\chunks\dep-0ed4fbc0.js:69982:166) {
code: 'ERR_REQUIRE_ESM'
}
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\workspace\m7d\vite-esm\node_modules\@raythurnevoid\strings-filter\index.js
require() of ES modules is not supported.
require() of C:\workspace\m7d\vite-esm\node_modules\@raythurnevoid\strings-filter\index.js from C:\workspace\m7d\vite-esm\node_modules\vite\dist\node\chunks\dep-0ed4fbc0.js is an ES module file as it is a .js file whose nearest parent package.json contains
"type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\workspace\m7d\vite-esm\node_modules\@raythurnevoid\strings-filter\package.json.
at new NodeError (node:internal/errors:363:5)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1126:13)
at Module.load (node:internal/modules/cjs/loader:989:32)
at Function.Module._load (node:internal/modules/cjs/loader:829:14)
at Module.require (node:internal/modules/cjs/loader:1013:19)
at require (node:internal/modules/cjs/helpers:93:18)
at nodeRequire (C:\workspace\m7d\vite-esm\node_modules\vite\dist\node\chunks\dep-0ed4fbc0.js:69996:17)
at ssrImport (C:\workspace\m7d\vite-esm\node_modules\vite\dist\node\chunks\dep-0ed4fbc0.js:69949:20)
at eval (/src/pages/Home.vue:7:31)
at instantiateModule (C:\workspace\m7d\vite-esm\node_modules\vite\dist\node\chunks\dep-0ed4fbc0.js:69982:166)
node:internal/process/promises:246
triggerUncaughtException(err, true /* fromPromise */);
^
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\workspace\m7d\vite-esm\node_modules\@raythurnevoid\strings-filter\index.js
require() of ES modules is not supported.
require() of C:\workspace\m7d\vite-esm\node_modules\@raythurnevoid\strings-filter\index.js from C:\workspace\m7d\vite-esm\node_modules\vite\dist\node\chunks\dep-0ed4fbc0.js is an ES module file as it is a .js file whose nearest parent package.json contains
"type": "module" which defines all .js files in that package scope as ES modules.
at new NodeError (node:internal/errors:363:5)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1126:13)
at Module.load (node:internal/modules/cjs/loader:989:32)
at Function.Module._load (node:internal/modules/cjs/loader:829:14)
at Module.require (node:internal/modules/cjs/loader:1013:19)
at require (node:internal/modules/cjs/helpers:93:18)
at ssrImport (C:\workspace\m7d\vite-esm\node_modules\vite\dist\node\chunks\dep-0ed4fbc0.js:69949:20)
at eval (/src/pages/Home.vue:7:31)
at instantiateModule (C:\workspace\m7d\vite-esm\node_modules\vite\dist\node\chunks\dep-0ed4fbc0.js:69982:166) {
code: 'ERR_REQUIRE_ESM'
}Before submitting the issue, please make sure you do the following
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Provide a description in this issue that describes the bug.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/vue-next instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.