Registry / web-framework / vite-plugin-treat-umd-as-commonjs

vite-plugin-treat-umd-as-commonjs

JSON →
library0.1.4jsnpmunverified

Vite plugin that resolves edge cases when using RequireJS/AMD in the browser by injecting `var define = false;` into UMD modules, preventing RequireJS from hijacking them. This addresses a specific issue where Vite's UMD-to-CommonJS conversion breaks in a browser RequireJS environment (vitejs/vite#5900). Current version 0.1.4, ships TypeScript types, requires Vite as peer dependency. Use this when you need to serve UMD modules via Vite but the page also includes RequireJS (e.g., legacy dependencies). The plugin is lightweight and scoped to this single concern.

npm install vite-plugin-treat-umd-as-commonjs
INSTALL
IMPORT
SIG · VITE-PLUGIN-TREAT-
V
vite-plugin-treat-umd-as-commonjs
web-frameworkjavascriptv0.1.4
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.

treatAsCommonjs
✓ import { treatAsCommonjs } from 'vite-plugin-treat-umd-as-commonjs'
✗ const treatAsCommonjs = require('vite-plugin-treat-umd-as-commonjs')
ESM-only since package ships TypeScript and uses ES modules.
Options
✓ import type { Options } from 'vite-plugin-treat-umd-as-commonjs'
✗ import { Options } from 'vite-plugin-treat-umd-as-commonjs'
Options is a TypeScript type export, not a runtime value. Use type import for TypeScript.
default import
✓ import treatAsCommonjs from 'vite-plugin-treat-umd-as-commonjs'
Default import is not exported; the package only exports named 'treatAsCommonjs'. Use named import.

Shows how to import and configure the plugin in a Vite config with include/exclude options.

// vite.config.ts import { defineConfig } from 'vite'; import { treatAsCommonjs } from 'vite-plugin-treat-umd-as-commonjs'; export default defineConfig({ plugins: [ treatAsCommonjs({ include: ['some-umd-module'], exclude: [/^some-esm-module/], }), ], });
Debug
Known issues
gotchaThe plugin adds `var define = false;` to modules, which will break any legitimate use of `define()` in scope (e.g., if you have custom AMD modules).
fix
Use `exclude` option to skip modules that should keep their AMD define.
affects: >=0.0.1
gotchaThe plugin only transforms modules from node_modules by default. If you have UMD modules in your source code, you must explicitly include them via the `include` option.
fix
Set `include` option to the file patterns that need treatment.
affects: >=0.0.1
deprecatedThe option `indluce` is a typo in the README; the actual option is `include`. Using `indluce` will be ignored.
fix
Use `include` instead of `indluce`.
affects: <=0.1.4
gotchaThis plugin may not work with Vite 3+ if the internal module resolution changes. It is tested against Vite 2.x mainly.
fix
Check compatibility with your Vite version; ensure tests pass.
affects: >=0.0.1
Errors
Common errors & fixes
Error: [commonjs–resolver] Could not detect entry module. RollupError: Could not resolve entry module.
The plugin interacts with Vite's CommonJS resolution and may conflict with other plugins or misconfigured include/exclude patterns.
fix
Ensure you are not including/excluding the entry module itself. Use more specific patterns.
Uncaught ReferenceError: define is not defined
RequireJS is not loaded but the plugin removed `var define = false;` (possibly overwritten) or you use a browser that doesn't have AMD support.
fix
Load RequireJS before your bundle or exclude the module from treatment.
Cannot find module 'vite-plugin-treat-umd-as-commonjs'
Package not installed or incorrect import path.
fix
Run `npm install vite-plugin-treat-umd-as-commonjs` and ensure you are using the named import `{ treatAsCommonjs }`.
Upgrade
Version history
0.1.4latest on npm
Audit
Dependencies
viterequiredpeer dependency required to function as a Vite plugin
Agent activity
17 hits · last 30 days
node
14
OpenAI (training)
1
Resources
vite-plugin-treat-umd-as-commonjs — npm install vite-plugin-treat-umd-as-commonjs · libregistry