Astro v4.13.1
Node v20.14.0
System Linux (x64)
Package Manager npm
Output static
Adapter none
Integrations none
(note that I'm commenting out cjs import to run `astro info` since otherwise it's breaking cli)
If this issue only occurs in one browser, which browser is a problem?
When cjs linked dependency is imported in astro.config.ts, it throws an error since Vite's ssrLoadModule doesn't support cjs global such as require and module.
When such dependency is only used inside app, users can workaround it by explicitly externalizing by vite.ssr.external, but that seems impossible when ssrLoadModule is used when loading astro.config.ts itself.
//// astro.config.ts
import { defineConfig } from 'astro/config';
// @ts-ignore
import testDepCjs from "test-dep-cjs"; // <-- this is linked dep
console.log(testDepCjs);
// https://astro.build/config
export default defineConfig({});
$ pnpm dev
> vite-ssr-cjs-astro-napi-rs-2206@0.0.1 dev /home/hiroshi/code/personal/reproductions/vite-ssr-cjs-astro-napi-rs-2206
> astro dev
12:03:37 PM [vite] Error when evaluating SSR module /fixtures/test-dep-cjs/index.js:
|- ReferenceError: require is not defined
at /home/hiroshi/code/personal/reproductions/vite-ssr-cjs-astro-napi-rs-2206/fixtures/test-dep-cjs/index.js:2:1
at instantiateModule (file:///home/hiroshi/code/personal/reproductions/vite-ssr-cjs-astro-napi-rs-2206/node_modules/.pnpm/vite@5.3.5/node_modules/vite/dist/node/chunks/dep-mCdpKltl.js:52650:11)
12:03:37 PM [vite] Error when evaluating SSR module /home/hiroshi/code/personal/reproductions/vite-ssr-cjs-astro-napi-rs-2206/astro.config.ts:
|- ReferenceError: require is not defined
at /home/hiroshi/code/personal/reproductions/vite-ssr-cjs-astro-napi-rs-2206/fixtures/test-dep-cjs/index.js:2:1
at instantiateModule (file:///home/hiroshi/code/personal/reproductions/vite-ssr-cjs-astro-napi-rs-2206/node_modules/.pnpm/vite@5.3.5/node_modules/vite/dist/node/chunks/dep-mCdpKltl.js:52650:11)
[astro] Unable to load your Astro config
What's the expected result?
Config loads successfully.
I'm not so familiar with Astro, but when looking at the code, it looks like it's desired/planned to externalize all deps here. Perhaps, this can be switched to ssr.external: true since the linked PR is already merged long time ago.
Yeah, we're planning to switch to ssr.external: true in the next major as it's a breaking change for existing users who relied on the previous behaviour.
Astro Info
If this issue only occurs in one browser, which browser is a problem?
NA
Describe the Bug
Related: https://github.com/napi-rs/napi-rs/issues/2206
When cjs linked dependency is imported in
astro.config.ts
, it throws an error since Vite'sssrLoadModule
doesn't support cjs global such asrequire
andmodule
.When such dependency is only used inside app, users can workaround it by explicitly externalizing by
vite.ssr.external
, but that seems impossible whenssrLoadModule
is used when loadingastro.config.ts
itself.What's the expected result?
Config loads successfully.
I'm not so familiar with Astro, but when looking at the code, it looks like it's desired/planned to externalize all deps here. Perhaps, this can be switched to
ssr.external: true
since the linked PR is already merged long time ago.https://github.com/withastro/astro/blob/bc2796436dc3810e988c27b71b7a66fcb1ae8bda/packages/astro/src/core/config/vite-load.ts#L14-L27
Link to Minimal Reproducible Example
https://stackblitz.com/github/hi-ogawa/reproductions/tree/main/vite-ssr-cjs-astro-napi-rs-2206?file=README.md
(note: on stackblitz, it looks like
ssrFixStacktrace
is also crashing so the error log is different. original code can be found in https://github.com/hi-ogawa/reproductions/tree/main/vite-ssr-cjs-astro-napi-rs-2206)Participation