Registry / devops / vite-plugin-cjs-interop

vite-plugin-cjs-interop

JSON →
library3.3.0jsnpmunverified

A Vite plugin that unwraps default imports from CommonJS (CJS) dependencies during SSR, solving the issue where CJS modules with both default and named exports require accessing the default via .default in ESM. Version 3.3.0 is current, with recent releases every few months. It works around Vite's SSR hoisting bug (vite#22122) and supports Vite 6.4–8. Automatically transforms import statements so that default imports work seamlessly in SSR, and handles dynamic imports to allow both ESM namespace and CJS direct property access. Competes with vite-plugin-commonjs but focuses specifically on default interop during SSR. ESM-only since v3 headless.

npm install vite-plugin-cjs-interop
INSTALL
IMPORT
SIG · VITE-PLUGIN-CJS-IN
V
vite-plugin-cjs-interop
devopsjavascriptv3.3.0
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
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 18–223 runs
build_error
glibc
node 18–223 runs
build_error
Code
Verified usage

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

cjsInterop
✓ import { cjsInterop } from 'vite-plugin-cjs-interop'
✗ const cjsInterop = require('vite-plugin-cjs-interop')
Package is ESM-only since v3 (no CommonJS release). require() will fail unless using a bundler that can handle ESM.
default (cjsInterop)
✓ import { cjsInterop } from 'vite-plugin-cjs-interop'
✗ import cjsInterop from 'vite-plugin-cjs-interop'
The package exports a named export `cjsInterop`, not a default export. Using default import will result in undefined.
Type (CjsInteropOptions)
✓ import type { CjsInteropOptions } from 'vite-plugin-cjs-interop'
✗ import { CjsInteropOptions } from 'vite-plugin-cjs-interop'
Options type is exported but only available as a type import. Using a value import will cause a runtime error.

Shows how to import, configure, and add the plugin to a Vite config, listing CJS dependencies that need interop.

import { defineConfig } from 'vite'; import { cjsInterop } from 'vite-plugin-cjs-interop'; export default defineConfig({ plugins: [ cjsInterop({ dependencies: [ 'some-cjs-package', 'another-package/deep', '@scope/**', ], }), ], });
Debug
Known issues
breakingv3.0.0 dropped CommonJS release; package is now ESM-only.
fix
Use ESM imports (import) instead of require(). If you must use CommonJS, stay on v2.x.
affects: >=3.0.0
breakingv3.0.0 dropped support for older Vite versions (requires ~6.4 || ~7.3 || 8).
fix
Upgrade Vite to a supported version (6.4+, 7.3+, or 8).
affects: >=3.0.0
gotchadependencies option requires explicit listing of deep imports; globs are supported but must be correctly configured.
fix
Ensure deep imports like 'pkg/subpath' are listed separately, or use a glob pattern like 'pkg/**'.
affects: *
gotchaThe plugin does not transform CJS dependencies that are not listed in the 'dependencies' option.
fix
Make sure every CJS dependency that needs default interop is included in the dependencies array.
affects: *
deprecatedNo known deprecations yet.
affects: none
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'default')
CJS dependency's default export is not being unwrapped because the package is not listed in the interop dependencies.
fix
Add the package name to the cjsInterop({ dependencies: ['your-package'] }) configuration.
ERR_REQUIRE_ESM: require() of ES Module /node_modules/vite-plugin-cjs-interop/dist/index.js from ... not supported.
Using require() to load the plugin, but it's ESM-only since v3.
fix
Use ESM imports (import { cjsInterop } from 'vite-plugin-cjs-interop') or downgrade to v2.x.
Uncaught TypeError: default_1.default is not a function
A CJS dependency's default export is being accessed as .default.default due to double wrapping.
fix
Ensure the package is correctly listed in cjsInterop dependencies, and avoid manually accessing .default on the import.
Upgrade
Version history
3.3.0latest on npm
Audit
Dependencies
viterequiredPeer dependency, only works with Vite.
Agent activity
22 hits · last 30 days
node
18
OpenAI (training)
2
Resources
vite-plugin-cjs-interop — npm install vite-plugin-cjs-interop · libregistry