{"id":20782,"library":"webpack-inject-plugin","title":"webpack-inject-plugin","description":"A webpack plugin to dynamically inject arbitrary code into the bundle at build time. Current stable version is 1.5.5, compatible with webpack >=4.0.0. It allows injection via a loader function (synchronous or async), with options for entry filtering and ordering (First, Last, NotLast). Unlike other injection plugins, it leverages webpack's loader context and supports multiple plugin instances with unique loader IDs. Useful for polyfills, instrumentation, or config-driven code injection.","status":"active","version":"1.5.5","language":"javascript","source_language":"en","source_url":"https://github.com/adierkens/webpack-inject-plugin","tags":["javascript","webpack","plugin","loader","inject"],"install":[{"cmd":"npm install webpack-inject-plugin","lang":"bash","label":"npm"},{"cmd":"yarn add webpack-inject-plugin","lang":"bash","label":"yarn"},{"cmd":"pnpm add webpack-inject-plugin","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency, plugin integrates with webpack's compilation process.","package":"webpack","optional":false}],"imports":[{"note":"The default export is the main plugin class. In CommonJS, you must use .default: const InjectPlugin = require('webpack-inject-plugin').default;","wrong":"const InjectPlugin = require('webpack-inject-plugin')","symbol":"default","correct":"import InjectPlugin from 'webpack-inject-plugin'"},{"note":"ENTRY_ORDER is a named export, not the default. It provides constants First, Last, NotLast.","wrong":"import ENTRY_ORDER from 'webpack-inject-plugin'","symbol":"ENTRY_ORDER","correct":"import { ENTRY_ORDER } from 'webpack-inject-plugin'"},{"note":"In CommonJS, the default export is accessed via .default because the package uses ESM-style exports.","wrong":"const InjectPlugin = require('webpack-inject-plugin')","symbol":"default (CommonJS)","correct":"const InjectPlugin = require('webpack-inject-plugin').default"}],"quickstart":{"code":"// webpack.config.js\nconst InjectPlugin = require('webpack-inject-plugin').default;\n\nmodule.exports = {\n  entry: './src/index.js',\n  output: { filename: 'bundle.js' },\n  plugins: [\n    new InjectPlugin(() => {\n      return \"console.log('Injected code!');\";\n    }, {\n      entryName: 'main',\n      entryOrder: require('webpack-inject-plugin').ENTRY_ORDER.Last\n    })\n  ]\n};","lang":"javascript","description":"Shows how to configure webpack-inject-plugin to inject console.log into the 'main' entry."},"warnings":[{"fix":"Use const InjectPlugin = require('webpack-inject-plugin').default;","message":"In CommonJS, the default export is not the plugin constructor; you must use require('webpack-inject-plugin').default.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Explicitly set entryOrder to ENTRY_ORDER.First or ENTRY_ORDER.Last if you need exact control.","message":"The entryOrder option defaults to ENTRY_ORDER.NotLast. If you rely on the default, you may get unexpected bundle order.","severity":"deprecated","affected_versions":">=1.0.0"},{"fix":"To inject global code, wrap it in self-executing function or assign to globalThis.","message":"The injected code runs in the bundle's module scope, not the global scope unless you explicitly assign to window or global.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Provide a unique loaderID option for each InjectPlugin instance if you use more than one.","message":"In v1.5.0, the loader ID handling changed. Multiple instances may conflict if IDs are not unique.","severity":"breaking","affected_versions":">=1.5.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Use const InjectPlugin = require('webpack-inject-plugin').default;","cause":"CommonJS require does not automatically resolve default export (ESM-style).","error":"TypeError: InjectPlugin is not a constructor"},{"fix":"Run npm install webpack-inject-plugin --save-dev and ensure webpack is >=4.","cause":"Missing installation or webpack version mismatch (requires webpack >=4.0.0).","error":"Module not found: Error: Can't resolve 'webpack-inject-plugin'"},{"fix":"Import with const { ENTRY_ORDER } = require('webpack-inject-plugin');","cause":"Imported incorrectly; ENTRY_ORDER is a named export.","error":"Error: ENTRY_ORDER is not defined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}