Registry / devops / vite-plugin-require-transform

vite-plugin-require-transform

JSON →
library1.0.21jsnpmunverified

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-transform
INSTALL
IMPORT
SIG · VITE-PLUGIN-REQUIR
V
vite-plugin-require-transform
devopsjavascriptv1.0.21
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18223 runs
build_error
glibc
node 18223 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default (requireTransform)
import requireTransform from 'vite-plugin-require-transform'
const requireTransform = require('vite-plugin-require-transform')
Package exports a CommonJS default export via ESM wrapper; named import { requireTransform } will not work.
VitePluginRequireTransformParamsType
import type { VitePluginRequireTransformParamsType } from 'vite-plugin-require-transform'
import { VitePluginRequireTransformParamsType } from 'vite-plugin-require-transform'
TypeScript type only; not available at runtime. Use 'import type' to avoid bundling issues.
plugin usage in vite.config.ts
plugins: [requireTransform({ fileRegex: /.[jt]sx?$/ })]
plugins: [requireTransform()]
Without options, default fileRegex matches .ts and .tsx only; for JavaScript or other files you must override.

Basic configuration of vite-plugin-require-transform in vite.config.ts, showing all available options with default overrides.

// vite.config.ts import { defineConfig } from 'vite'; import requireTransform from 'vite-plugin-require-transform'; export default defineConfig({ plugins: [ requireTransform({ fileRegex: /\.(js|ts|jsx|tsx)$/, importPrefix: '__req_', importPathHandler: (path: string) => path.replace(/\?.*$/, '') }) ] });
Debug
Known issues
breakingDynamic require() (e.g., require(variable)) is not supported and will throw at runtime.
fix
Replace dynamic require() with dynamic import() or static import.
affects: all
gotchaTransformation only applies to files matching fileRegex (default .ts/.tsx). JS files are ignored unless you set fileRegex to include them.
fix
Explicitly set fileRegex: /\.(js|ts|jsx|tsx)$/ in plugin options.
affects: all
gotchaWhen require is used with a ternary or conditional, the resulting import may produce named exports that don't exist, causing runtime errors.
fix
Assign require() result to a const even in conditional branches to ensure correct import extraction.
affects: all
deprecatedNo known deprecated patterns yet; package is still actively maintained.
affects: none
Errors
Common errors & fixes
SyntaxError: Unexpected token '.' (or similar parsing errors)
Plugin tries to transform require inside nested expressions it cannot handle (e.g., require('x').y.z).
fix
Rewrite to const x = require('x'); x.y.z; or use importPathHandler to simplify the expression.
Error: The plugin 'vite-plugin-require-transform' doesn't seem to be a valid Vite plugin.
Importing the plugin incorrectly (e.g., as named export instead of default).
fix
Use 'import requireTransform from 'vite-plugin-require-transform''.
Uncaught ReferenceError: require is not defined
Plugin did not transform the file (file not matching regex or plugin not added).
fix
Check that fileRegex includes the file's extension and that plugin is in the plugins array.
Upgrade
Version history
1.0.21latest on npm
Audit
Dependencies
viterequiredplugin runs as a Vite plugin
Agent activity
6 hits · last 30 days
node
6
Resources
vite-plugin-require-transform — npm install vite-plugin-require-transform · libregistry