Back to all fixes
CuratedTypeScriptVite 8local

Vite 8: dynamic import() of a .cjs file inside application source code stops working

Problem

Application source code that does `await import('./something.cjs')` (dynamic import of a CommonJS file, e.g. to run Node.js-target code) worked in Vite 7 but fails in Vite 8. Root cause: it only happened to work before because `@rollup/plugin-commonjs` hardcoded handling of `.cjs` files regardless of Vite's configured filter; Rolldown's automatic CJS handling now incorrectly treats the dynamic import as a static import instead of leaving it alone, and Vite never officially supported dynamically importing raw CJS from source in the first place (dev mode never worked with this pattern either).

Solution

typescript
Confirmed by Vite maintainer bluwy: this is not supported and there is no vite-side fix - Vite's philosophy requires source code to be ESM; only installed dependencies may be CJS. The reporter confirmed working around it by removing/replacing the single dynamic `import()` of a `.cjs` file in their app. If your use case specifically targets building for Node.js (not the browser), use a Node-targeted bundler such as `tsdown` for that entry point instead of Vite.