{"id":22893,"library":"vite-plugin-treat-umd-as-commonjs","title":"vite-plugin-treat-umd-as-commonjs","description":"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.","status":"active","version":"0.1.4","language":"javascript","source_language":"en","source_url":"https://github.com/pakholeung37/vite-plugin-treat-umd-as-commonjs","tags":["javascript","vite","vite-plugin","commonjs","cjs","umd","requirejs","require-js","amd","typescript"],"install":[{"cmd":"npm install vite-plugin-treat-umd-as-commonjs","lang":"bash","label":"npm"},{"cmd":"yarn add vite-plugin-treat-umd-as-commonjs","lang":"bash","label":"yarn"},{"cmd":"pnpm add vite-plugin-treat-umd-as-commonjs","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"peer dependency required to function as a Vite plugin","package":"vite","optional":false}],"imports":[{"note":"ESM-only since package ships TypeScript and uses ES modules.","wrong":"const treatAsCommonjs = require('vite-plugin-treat-umd-as-commonjs')","symbol":"treatAsCommonjs","correct":"import { treatAsCommonjs } from 'vite-plugin-treat-umd-as-commonjs'"},{"note":"Options is a TypeScript type export, not a runtime value. Use type import for TypeScript.","wrong":"import { Options } from 'vite-plugin-treat-umd-as-commonjs'","symbol":"Options","correct":"import type { Options } from 'vite-plugin-treat-umd-as-commonjs'"},{"note":"Default import is not exported; the package only exports named 'treatAsCommonjs'. Use named import.","wrong":"","symbol":"default import","correct":"import treatAsCommonjs from 'vite-plugin-treat-umd-as-commonjs'"}],"quickstart":{"code":"// vite.config.ts\nimport { defineConfig } from 'vite';\nimport { treatAsCommonjs } from 'vite-plugin-treat-umd-as-commonjs';\n\nexport default defineConfig({\n  plugins: [\n    treatAsCommonjs({\n      include: ['some-umd-module'],\n      exclude: [/^some-esm-module/],\n    }),\n  ],\n});","lang":"typescript","description":"Shows how to import and configure the plugin in a Vite config with include/exclude options."},"warnings":[{"fix":"Use `exclude` option to skip modules that should keep their AMD define.","message":"The 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).","severity":"gotcha","affected_versions":">=0.0.1"},{"fix":"Set `include` option to the file patterns that need treatment.","message":"The 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.","severity":"gotcha","affected_versions":">=0.0.1"},{"fix":"Use `include` instead of `indluce`.","message":"The option `indluce` is a typo in the README; the actual option is `include`. Using `indluce` will be ignored.","severity":"deprecated","affected_versions":"<=0.1.4"},{"fix":"Check compatibility with your Vite version; ensure tests pass.","message":"This plugin may not work with Vite 3+ if the internal module resolution changes. It is tested against Vite 2.x mainly.","severity":"gotcha","affected_versions":">=0.0.1"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"Ensure you are not including/excluding the entry module itself. Use more specific patterns.","cause":"The plugin interacts with Vite's CommonJS resolution and may conflict with other plugins or misconfigured include/exclude patterns.","error":"Error: [commonjs–resolver] Could not detect entry module. RollupError: Could not resolve entry module."},{"fix":"Load RequireJS before your bundle or exclude the module from treatment.","cause":"RequireJS is not loaded but the plugin removed `var define = false;` (possibly overwritten) or you use a browser that doesn't have AMD support.","error":"Uncaught ReferenceError: define is not defined"},{"fix":"Run `npm install vite-plugin-treat-umd-as-commonjs` and ensure you are using the named import `{ treatAsCommonjs }`.","cause":"Package not installed or incorrect import path.","error":"Cannot find module 'vite-plugin-treat-umd-as-commonjs'"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}