Dynamic import resolution in esbuild bundling requires deep understanding of both esbuild and runtime environments.
The issue involves esbuild failing to resolve dynamic imports during bundling, particularly affecting Node.js runtime compatibility. It requires understanding esbuild's bundling behavior and potential workarounds for dynamic imports. The solution may involve significant changes to how imports are handled or dropping Node 18 support.
This is related to #17762 .
Looking back it should've been obvious... since the static analyzer no longer sees the import() call, esbuild no longer resolves it in --bundle mode (docs), so the resulting source code looks like this:
var obfuscated_import = (module_name) => import(
/* @vite-ignore */
module_name
);
There's obviously no error at build time, it just hits you at runtime when running the .cjs file because... well, esbuild is supposed to resolve the import(), but it's not, and a cjs only runtime has no idea what import() is.
I honestly don't know if this is fixable unless node:crypto is dropped, aka node 18 is dropped.
Another alternative would be to use a userland crypto package instead of something from the platform, but I don't know if that's doable or even worth it, this is getting ridiculous.
I'll come up with some patch-work on my end, I'll probably have to straight up remove that code from the bundle and cross my fingers.
I'm just opening this because I thought I should let you guys know about the issue.
Full reproduction can be found here https://github.com/razshare/svelte-reproduction-issue-17771
make build
npx vite build --logLevel=info --outDir=dist/server --emptyOutDir=true --ssr=app.server.ts
vite v7.3.1 building ssr environment for production...
✓ 114 modules transformed.
dist/server/app.server.js 32.32 kB
✓ built in 249ms
./node_modules/.bin/esbuild --bundle --outfile=dist/server/app.server.cjs --format=cjs --allow-overwrite dist/server/app.server.js
dist/server/app.server.cjs 46.1kb
⚡ Done in 4ms
System:
OS: Linux 6.12 Debian GNU/Linux 13 (trixie) 13 (trixie)
CPU: (32) x64 AMD Ryzen 9 5950X 16-Core Processor
Memory: 112.19 GB / 125.69 GB
Container: Yes
Shell: 5.2.37 - /bin/bash
Binaries:
Node: 24.12.0 - /home/raz/.nvm/versions/node/v24.12.0/bin/n
Claim this issue to let others know you're working on it. You'll earn 30 points when you complete it!