A Vite plugin that converts CommonJS require() calls to ES module import syntax at build time, enabling codebases that use require() to work with Vite's ESM-native dev server. Current stable version: 1.0.21. Released as needed, with occasional minor updates. Key differentiators: supports complex cases like ternary expressions, method calls on require results, and custom import path handlers; does not require source code changes; filters by file regex (default .ts/.tsx) to avoid unnecessary transformations. Alternatives like @originjs/vite-plugin-commonjs or vite-plugin-commonjs-externals may handle broader CJS interop, but this plugin is minimal and focused specifically on require → import.
npm install vite-plugin-require-transformVerified import paths — ran on the pinned version, not inferred.
Basic configuration of vite-plugin-require-transform in vite.config.ts, showing all available options with default overrides.
Replace dynamic require() with dynamic import() or static import.
Explicitly set fileRegex: /\.(js|ts|jsx|tsx)$/ in plugin options.
Assign require() result to a const even in conditional branches to ensure correct import extraction.
Rewrite to const x = require('x'); x.y.z; or use importPathHandler to simplify the expression.Use 'import requireTransform from 'vite-plugin-require-transform''.
Check that fileRegex includes the file's extension and that plugin is in the plugins array.