Vite plugin that prevents bundling of specified ESM imports and replaces them with CommonJS require() calls at runtime. Version 0.1.4, stable with infrequent releases. Designed for Electron renderer processes and Node.js environments where CommonJS modules (like 'electron', 'fs', 'path') must be imported via require() rather than bundled. Key differentiator: handles named, default, and namespace imports correctly by generating interop code similar to Rollup's CJS output. Unlike @vitejs/plugin-commonjs which focuses on converting CJS to ESM, this plugin does the reverse — converting ESM imports into CJS requires for a list of externals. Supports regex patterns for dynamic package matching.
npm install vite-plugin-commonjs-externalsVerified import paths — ran on the pinned version, not inferred.
Configures Vite to treat 'path' and 'electron' packages as CommonJS externals, preventing them from being bundled.
Always include the same list in optimizeDeps.exclude as shown in the quickstart.
Ensure external packages are imported with static import statements, not require().
Do not use this plugin in SSR configurations; use Vite's built-in SSR externals instead.
Add type stubs or set 'skipLibCheck: true' in tsconfig.json.
Upgrade Vite to ^4.3.9 or ^5.0.0 which are known to work.
Add the package to optimizeDeps.exclude in vite.config.js.
Use import commonjsExternals from 'vite-plugin-commonjs-externals' (ESM default import).