Registry / web-framework / webpack-virtual-modules

webpack-virtual-modules

JSON →
library0.6.2jsnpmunverified

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.

npm install webpack-virtual-modules
INSTALL
IMPORT
SIG · WEBPACK-VIRTUAL-MO
W
webpack-virtual-modules
web-frameworkjavascriptv0.6.2
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.

VirtualModulesPlugin
import VirtualModulesPlugin from 'webpack-virtual-modules';
const VirtualModulesPlugin = require('webpack-virtual-modules').default;
Default export; no named exports. In CJS, require returns the plugin directly (no .default).
VirtualModulesPlugin (new)
const VirtualModulesPlugin = require('webpack-virtual-modules');
const { VirtualModulesPlugin } = require('webpack-virtual-modules');
CommonJS: destructuring fails because it's a default export. Use full require.
webpack config integration
plugins: [new VirtualModulesPlugin({ 'path/to/module.js': 'module.exports = ...' })]
plugins: [new VirtualModulesPlugin().writeModule('path/to/module.js', '...')]
Old API deprecated. Provide modules as object in constructor.

Basic setup with webpack to create a virtual module available as a dependency.

const VirtualModulesPlugin = require('webpack-virtual-modules'); const virtualModules = new VirtualModulesPlugin({ 'node_modules/virtual-module.js': 'module.exports = { foo: "bar" };', }); module.exports = { // ... other config plugins: [ virtualModules, // other plugins ], };
Debug
Known issues
deprecatedOld API: writeModule() and deleteModule() methods are deprecated in favor of constructor-based module definitions.
fix
Pass an object to the constructor: new VirtualModulesPlugin({ 'path': 'content' }).
affects: >=0.10.0
gotchaVirtual modules must have paths that resolve to valid module identifiers (e.g., absolute paths or relative to node_modules).
fix
Use paths like 'node_modules/my-module.js' or absolute paths.
affects: >=0
breakingWebpack 5 compatibility: fs access method changed. Older versions of this plugin may not work with webpack 5.
fix
Update to v0.11.0 or later which supports webpack 5.
affects: <0.11.0
deprecatedThe `writeModule` method is deprecated as of v0.9.0.
fix
Use constructor options or call `virtualModules.writeModule()` only if necessary.
affects: >=0.9.0
gotchaDynamic module content updates: Modifying virtual modules after compilation requires calling `invalidate()` on the plugin.
fix
Call `virtualModules.invalidate()` after updating content using `writeModule()`.
affects: >=0
Errors
Common errors & fixes
Error: Cannot find module 'virtual-module'
Virtual module path not resolved correctly by webpack
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.
TypeError: VirtualModulesPlugin is not a constructor
Wrong import style: using destructuring when the export is default
fix
Use `const VirtualModulesPlugin = require('webpack-virtual-modules');` or `import VirtualModulesPlugin from 'webpack-virtual-modules';`
Error: webpack-virtual-modules: you must provide a virtual modules map to the constructor
Calling constructor without arguments when using the new API
fix
Pass an object as the first argument: `new VirtualModulesPlugin({ './src/foo.js': '...' })`
Upgrade
Version history
0.6.2latest on npm
Audit
Dependencies
webpackrequiredpeer dependency; requires webpack 4 or 5
Agent activity
10 hits · last 30 days
node
8
OpenAI (training)
1
Resources
webpack-virtual-modules — npm install webpack-virtual-modules · libregistry