{"id":20851,"library":"webpack-virtual-modules","title":"webpack-virtual-modules","description":"Webpack plugin that allows you to create virtual (in-memory) modules at compile time. Current stable version 0.6.2. Updated sporadically; last release Nov 2023. Key differentiator: enables injecting custom modules that don't exist on disk, useful for runtime-generated code or mocking, without modifying the filesystem. Supports webpack 4 & 5. Alternatives like virtual-module-webpack-plugin or inline-source-map have different APIs or less maintenance.","status":"active","version":"0.6.2","language":"javascript","source_language":"en","source_url":"https://github.com/sysgears/webpack-virtual-modules","tags":["javascript","webpack","webpack-plugin","virtual","modules"],"install":[{"cmd":"npm install webpack-virtual-modules","lang":"bash","label":"npm"},{"cmd":"yarn add webpack-virtual-modules","lang":"bash","label":"yarn"},{"cmd":"pnpm add webpack-virtual-modules","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"peer dependency; requires webpack 4 or 5","package":"webpack","optional":false}],"imports":[{"note":"Default export; no named exports. In CJS, require returns the plugin directly (no .default).","wrong":"const VirtualModulesPlugin = require('webpack-virtual-modules').default;","symbol":"VirtualModulesPlugin","correct":"import VirtualModulesPlugin from 'webpack-virtual-modules';"},{"note":"CommonJS: destructuring fails because it's a default export. Use full require.","wrong":"const { VirtualModulesPlugin } = require('webpack-virtual-modules');","symbol":"VirtualModulesPlugin (new)","correct":"const VirtualModulesPlugin = require('webpack-virtual-modules');"},{"note":"Old API deprecated. Provide modules as object in constructor.","wrong":"plugins: [new VirtualModulesPlugin().writeModule('path/to/module.js', '...')]","symbol":"webpack config integration","correct":"plugins: [new VirtualModulesPlugin({ 'path/to/module.js': 'module.exports = ...' })]"}],"quickstart":{"code":"const VirtualModulesPlugin = require('webpack-virtual-modules');\n\nconst virtualModules = new VirtualModulesPlugin({\n  'node_modules/virtual-module.js': 'module.exports = { foo: \"bar\" };',\n});\n\nmodule.exports = {\n  // ... other config\n  plugins: [\n    virtualModules,\n    // other plugins\n  ],\n};","lang":"javascript","description":"Basic setup with webpack to create a virtual module available as a dependency."},"warnings":[{"fix":"Pass an object to the constructor: new VirtualModulesPlugin({ 'path': 'content' }).","message":"Old API: writeModule() and deleteModule() methods are deprecated in favor of constructor-based module definitions.","severity":"deprecated","affected_versions":">=0.10.0"},{"fix":"Use paths like 'node_modules/my-module.js' or absolute paths.","message":"Virtual modules must have paths that resolve to valid module identifiers (e.g., absolute paths or relative to node_modules).","severity":"gotcha","affected_versions":">=0"},{"fix":"Update to v0.11.0 or later which supports webpack 5.","message":"Webpack 5 compatibility: fs access method changed. Older versions of this plugin may not work with webpack 5.","severity":"breaking","affected_versions":"<0.11.0"},{"fix":"Use constructor options or call `virtualModules.writeModule()` only if necessary.","message":"The `writeModule` method is deprecated as of v0.9.0.","severity":"deprecated","affected_versions":">=0.9.0"},{"fix":"Call `virtualModules.invalidate()` after updating content using `writeModule()`.","message":"Dynamic module content updates: Modifying virtual modules after compilation requires calling `invalidate()` on the plugin.","severity":"gotcha","affected_versions":">=0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Ensure the virtual module path is an absolute path or starts with './' or '../' relative to project root. Use path.resolve() to construct absolute paths.","cause":"Virtual module path not resolved correctly by webpack","error":"Error: Cannot find module 'virtual-module'"},{"fix":"Use `const VirtualModulesPlugin = require('webpack-virtual-modules');` or `import VirtualModulesPlugin from 'webpack-virtual-modules';`","cause":"Wrong import style: using destructuring when the export is default","error":"TypeError: VirtualModulesPlugin is not a constructor"},{"fix":"Pass an object as the first argument: `new VirtualModulesPlugin({ './src/foo.js': '...' })`","cause":"Calling constructor without arguments when using the new API","error":"Error: webpack-virtual-modules: you must provide a virtual modules map to the constructor"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}