Closed
Description
Describe the bug
Part of the heuristic for determining if a dependency should be externalized for SSR is to compare the "resolved ESM entry point and its default Node entry point". That comparison happens here:
The path
requireEntry
comes from require.resolve
which is not normalized. On Windows the path entry
(which is normalized) has POSIX path separators and requireEntry
has Windows path separators. This leads to a false positive, thinking the package has a cjs distribution which then fails at runtime.
Simply using Vite's path normalization utility on requireEntry
also doesn't quite work as, at least in my case, the paths end up with slightly different casing: "C:\..."
vs "c:\..."
Reproduction
https://github.com/rturnq/solid-hackernews
npm i
npm run dev
System Info
vite
version: 2.0.5- Operating System: Windows 10.0.18363
- Node version: 15.4.0
- Package manager (npm/yarn/pnpm) and version: npm 7.0.15
Logs (Optional if provided reproduction)
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\Some\File\Path\solid-hackernews\node_modules\solid-app-router\dist\index.js
require() of ES modules is not supported.
require() of C:\Some\File\Path\solid-hackernews\node_modules\solid-app-router\dist\index.js from C:\Some\File\Path\solid-hackernews\node_modules\vite\dist\node\chunks\dep-e0f09032.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:\Some\File\Path\solid-hackernews\node_modules\solid-app-router\package.json.
at new NodeError (node:internal/errors:278:15)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1125:13)
at Module.load (node:internal/modules/cjs/loader:973:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Module.require (node:internal/modules/cjs/loader:997:19)
at n (C:\Some\File\Path\solid-hackernews\node_modules\jiti\dist\v8cache.js:2:2349)
at nodeRequire (C:\Some\File\Path\solid-hackernews\node_modules\vite\dist\node\chunks\dep-e0f09032.js:68055:17)
at ssrImport (C:\Some\File\Path\solid-hackernews\node_modules\vite\dist\node\chunks\dep-e0f09032.js:68013:20)
at eval (/src/components/nav.tsx:15:31)
at instantiateModule (C:\Some\File\Path\solid-hackernews\node_modules\vite\dist\node\chunks\dep-e0f09032.js:68041:166)