vite-plugin-require-support
raw JSON → 1.6.1 verified Mon Apr 27 auth: no javascript
Vite plugin enabling require() syntax in Vite projects by transforming CommonJS require calls to ESM imports. Current stable version 1.6.1, with frequent bug-fix releases. Key differentiator: specifically for Vite, unlike generic bundler plugins. It handles string literal and template literal require paths, generates hashed variable names to avoid collisions, and supports complex export patterns. Alternatives: @originjs/vite-plugin-commonjs or using esbuild's require polyfill.
Common errors
error Error [ERR_REQUIRE_ESM]: require() of ES Module ↓
cause Using require() on an ESM-only package (v1.1.16+).
fix
Change your config file extension to .mjs or use dynamic import: const requireSupport = (await import('vite-plugin-require-support')).default;
error require is not defined ↓
cause Vite runs in ESM mode by default; require is not available globally without plugin transformation.
fix
Ensure the plugin is added to vite.config and filters match your file extensions.
error Dynamic require of "..." is not supported ↓
cause Dynamic template literal require() where path cannot be statically analyzed.
fix
Replace with static require (string literal) or import().
Warnings
gotcha Plugin transforms require() at build time; dynamic require() with template literals may fail if the path is not statically analyzable. ↓
fix Use string literals for require() paths; avoid runtime-concatenated strings.
breaking Package is ESM-only since v1.1.16. require() in Node.js scripts (e.g., vite.config.cjs) will fail unless using dynamic import or .mjs extension. ↓
fix Use ES module syntax in your config: import requireSupport from 'vite-plugin-require-support';
gotcha filters option expects a RegExp, not a string or array of strings. Incorrect type may silently fail to match any files. ↓
fix Pass a regex literal: filters: /\.js$|\mjs$/ or new RegExp('.js$').
deprecated Option 'include' was used in early versions but renamed to 'filters'. Using 'include' may be ignored. ↓
fix Use 'filters' instead.
gotcha Plugin may not handle all CommonJS patterns like module.exports assignments or circular dependencies. Test thoroughly with your codebase. ↓
fix For complex CJS modules, consider alternative bundler setups or migrate to ESM.
Install
npm install vite-plugin-require-support yarn add vite-plugin-require-support pnpm add vite-plugin-require-support Imports
- requireSupport wrong
const requireSupport = require('vite-plugin-require-support')correctimport requireSupport from 'vite-plugin-require-support' - plugin wrong
export default defineConfig({ plugins: [require('vite-plugin-require-support')()] })correctimport requireSupport from 'vite-plugin-require-support'; export default defineConfig({ plugins: [requireSupport()] }) - filters wrong
requireSupport({ include: /.ts$/ })correctrequireSupport({ filters: /.ts$|.js$/ }) - types wrong
import requireSupport from 'vite-plugin-require-support' (if types-only)correctimport type requireSupport from 'vite-plugin-require-support'
Quickstart
// vite.config.ts
import { defineConfig } from 'vite';
import requireSupport from 'vite-plugin-require-support';
export default defineConfig({
plugins: [
requireSupport({
filters: /.ts$|.js$|.tsx$|.vue$/
})
]
});
// some-module.ts
const fs = require('fs');
const path = require('path');
const config = require('./config.json');
console.log(fs.readFileSync(path.join(__dirname, 'file.txt'), 'utf8'));