Registry / devops / html-replace-webpack-plugin

html-replace-webpack-plugin

JSON →
library2.6.0jsnpmunverified

A Webpack plugin that replaces HTML content during build using string or regex patterns. Currently at version 2.6.0 with a low release cadence (last update in 2019). It works exclusively with html-webpack-plugin, processing the output of that plugin. Allows single or multiple replacement rules with string or function replacements. Lightweight alternative to heavier string replacement tasks in Webpack. Only tested with Webpack 4.x; no support for Webpack 5 or ESM.

npm install html-replace-webpack-plugin
INSTALL
IMPORT
SIG · HTML-REPLACE-WEBPA
H
html-replace-webpack-plugin
devopsjavascriptv2.6.0
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 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
const HtmlReplaceWebpackPlugin = require('html-replace-webpack-plugin')
import HtmlReplaceWebpackPlugin from 'html-replace-webpack-plugin'
Package is CJS only; ESM import will fail. Use require() in Webpack config.
HtmlReplaceWebpackPlugin
const HtmlReplaceWebpackPlugin = require('html-replace-webpack-plugin')
const { HtmlReplaceWebpackPlugin } = require('html-replace-webpack-plugin')
Plugin is exported as a single default function, not a named export. Named destructuring returns undefined.
Plugin usage
new HtmlReplaceWebpackPlugin([{ pattern: 'foo', replacement: 'bar' }])
new HtmlReplaceWebpackPlugin({ pattern: 'foo', replacement: 'bar' })
Passing a single object instead of an array works (accepts either), but passing multiple arguments as separate objects fails. Use array for multiple rules.

Shows how to configure html-replace-webpack-plugin with html-webpack-plugin to replace HTML comments with CDN links for CSS/JS.

// webpack.config.js const HtmlWebpackPlugin = require('html-webpack-plugin'); const HtmlReplaceWebpackPlugin = require('html-replace-webpack-plugin'); const resource = { js: { bootstrap: '//cdn/bootstrap/bootstrap.min.js' }, css: { bootstrap: '//cdn/bootstrap/bootstrap.min.css' }, img: { 'the-girl': '//cdn/img/the-girl.jpg' } }; const tpl = { img: '<img src="%s">', css: '<link rel="stylesheet" type="text/css" href="%s">', js: '<script type="text/javascript" src="%s"></script>' }; module.exports = { entry: './src/index.js', plugins: [ new HtmlWebpackPlugin({ title: 'My App', template: './src/index.html' }), new HtmlReplaceWebpackPlugin([ { pattern: 'foo', replacement: 'bar' }, { pattern: '@@title', replacement: 'My App' }, { pattern: /(<!--\s*|@@)(css|js|img):([\w-\/]+)(\s*-->)?/g, replacement: function(match, $1, type, file, $4, index, input) { var url = resource[type][file]; return $4 == undefined ? url : tpl[type].replace('%s', url); } } ]) ] };
Debug
Known issues
breakinghtml-webpack-plugin must be placed before html-replace-webpack-plugin in plugins array (Webpack 4).
fix
Ensure HtmlWebpackPlugin is instantiated before HtmlReplaceWebpackPlugin in the plugins array.
affects: >=1.0.0
gotchaPlugin only processes HTML generated by html-webpack-plugin; not standalone HTML files.
fix
Use html-webpack-plugin and ensure it processes the target HTML files.
affects: >=1.0.0
gotchaNamed import or destructured require fails: const { HtmlReplaceWebpackPlugin } = require(...) => TypeError: HtmlReplaceWebpackPlugin is not a constructor.
fix
Use default require: const HtmlReplaceWebpackPlugin = require('html-replace-webpack-plugin').
affects: >=1.0.0
deprecatedLast release 2.6.0 (2019); no Webpack 5 support; may break with newer versions.
fix
Consider alternatives like string-replace-loader or custom hooks for Webpack 5.
affects: >=2.0.0
gotchaPassing multiple rules as separate arguments (e.g., new Plugin(rule1, rule2)) fails; only first rule processed.
fix
Wrap rules in an array: new Plugin([rule1, rule2]).
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: HtmlReplaceWebpackPlugin is not a constructor
Correctly importing as default but often caused by spelling or named import.
fix
const HtmlReplaceWebpackPlugin = require('html-replace-webpack-plugin');
The 'pattern' property is required for each replacement object.
Passing an empty object or missing pattern field.
fix
Ensure each object has a 'pattern' and 'replacement' property.
HtmlWebpackPlugin is not defined
Missing installation or require of html-webpack-plugin.
fix
npm i -D html-webpack-plugin; then require it before use.
Cannot find module 'html-replace-webpack-plugin'
Plugin not installed or not in node_modules.
fix
npm i -D html-replace-webpack-plugin
Upgrade
Version history
2.6.0latest on npm
Audit
Dependencies
html-webpack-pluginrequiredRequired peer dependency; plugin operates on the output of html-webpack-plugin.
Agent activity
4 hits · last 30 days
node
4
Resources
html-replace-webpack-plugin — npm install html-replace-webpack-plugin · libregistry