Registry / devops / vite-plugin-require-support

vite-plugin-require-support

JSON →
library1.6.1jsnpmunverified

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.

npm install vite-plugin-require-support
INSTALL
IMPORT
SIG · VITE-PLUGIN-REQUIR
V
vite-plugin-require-support
devopsjavascriptv1.6.1
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.

requireSupport
import requireSupport from 'vite-plugin-require-support'
const requireSupport = require('vite-plugin-require-support')
Default export; package is ESM-only from v1.1.16 due to 'type':'module' in package.json.
plugin
import requireSupport from 'vite-plugin-require-support'; export default defineConfig({ plugins: [requireSupport()] })
export default defineConfig({ plugins: [require('vite-plugin-require-support')()] })
Must import default export at top level, not inline require.
filters
requireSupport({ filters: /.ts$|.js$/ })
requireSupport({ include: /.ts$/ })
Option is named 'filters' (a regex), not 'include'. Default filters: /.ts$|.js$|.tsx$|.vue$/.
types
import type requireSupport from 'vite-plugin-require-support'
import requireSupport from 'vite-plugin-require-support' (if types-only)
Package ships TypeScript types, but type import is optional when using as plugin.

Basic setup: add plugin to Vite config, then use require() in TypeScript/JavaScript modules to import Node built-ins and local files.

// 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'));
Debug
Known issues
gotchaPlugin 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.
affects: *
breakingPackage 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';
affects: >=1.1.16
gotchafilters 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$').
affects: *
deprecatedOption 'include' was used in early versions but renamed to 'filters'. Using 'include' may be ignored.
fix
Use 'filters' instead.
affects: <1.0.0
gotchaPlugin 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.
affects: *
Errors
Common errors & fixes
Error [ERR_REQUIRE_ESM]: require() of ES Module
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;
require is not defined
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.
Dynamic require of "..." is not supported
Dynamic template literal require() where path cannot be statically analyzed.
fix
Replace with static require (string literal) or import().
Upgrade
Version history
1.6.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
vite-plugin-require-support — npm install vite-plugin-require-support · libregistry