Registry / devops / string-replace-loader

string-replace-loader

JSON →
library3.3.0jsnpmunverified

A Webpack loader that performs string or regex replacements on file contents during the build process. Version 3.3.0 is the latest stable release, with occasional minor/patch updates. It supports plain string replacement, RegExp replacement (via string with flags or RegExp object), multiple replacements in a single rule, callback-based dynamic replacement, and strict mode. Key differentiators: flexible configuration similar to String.prototype.replace, array of replacements, strict mode to ensure replacements occurred. Compared to alternatives like `replace-in-file-webpack-plugin` or `webpack-replace-loader`, this loader integrates directly into the loader chain and works per-file based on test patterns. Requires Webpack 5+ and Node.js 4+.

npm install string-replace-loader
INSTALL
IMPORT
SIG · STRING-REPLACE-LOA
S
string-replace-loader
devopsjavascriptv3.3.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.

string-replace-loader
module.exports = { module: { rules: [ { test: /\.js$/, loader: 'string-replace-loader', options: { search: 'foo', replace: 'bar' } } ] } }
import stringReplaceLoader from 'string-replace-loader'
This is a Webpack loader, not a regular library. It is referenced by the 'loader' field in Webpack config rules. No import statement needed; just configure in webpack.config.js.

Demonstrates basic replacement of 'jQuery' with 'window.$' in all .js files.

// webpack.config.js module.exports = { module: { rules: [ { test: /\.js$/, loader: 'string-replace-loader', options: { search: 'jQuery', replace: 'window.$' } } ] } };
Debug
Known issues
breakingVersion 3.0.0 drops support for Webpack 4 and lower. Using with Webpack 4 will cause errors.
fix
Upgrade to Webpack 5 or use string-replace-loader@2.3.0 for Webpack 4 support.
affects: >=3.0.0
breakingVersion 2.0.0 drops support for Node.js 3 and lower. Older Node versions will fail to load the loader.
fix
Upgrade Node.js to v4+ or use version 1.3.0.
affects: >=2.0.0
deprecatedThe loader uses String.prototype.replace internally; for global replacements, you must use a regex with the 'g' flag. Missing 'g' flag replaces only first occurrence.
fix
Set options.flags = 'g' or provide a RegExp with global flag.
affects: all
gotchaWhen using options.search as a string and options.flags, special regex characters in the search string must be escaped. If you intend a literal string, use the plain replacement method without flags.
fix
Either use a RegExp object or escape special characters like '\\$' for '$'.
affects: all
gotchaThe multiple replacements option processes replacements in parallel? No, they are applied sequentially. This can cause unexpected results if later replacements match strings introduced by earlier ones.
fix
Order your replacements carefully or use separate rules.
affects: >=2.0.0
Errors
Common errors & fixes
Module parse failed: Unexpected token (1:0) You may need an appropriate loader to handle this file type.
The string-replace-loader is misconfigured or not matching the correct file type, causing Webpack to attempt parsing the output as JavaScript.
fix
Ensure the loader is applied only to files that should be replaced, using a specific test pattern. Also ensure your rules are not overlapping incorrectly.
Error: The 'search' option is not defined, but 'replace' option is provided.
When not using 'multiple' replacements, both 'search' and 'replace' are required. This error occurs if 'search' is missing.
fix
Add a 'search' option to your loader configuration: options: { search: 'string', replace: 'newString' }
Error: Nothing replaced. Enable strict mode to throw on unreplaced content.
Strict mode is enabled (options.strict: true) but the search pattern was not found in the file content.
fix
Disable strict mode or ensure the search pattern exists in the matched files.
TypeError: loaderContext.getOptions is not a function
Using an incompatible version of Webpack (e.g., Webpack 4 with loader version 3.x).
fix
Use string-replace-loader@2.3.0 for Webpack 4 or upgrade to Webpack 5.
Upgrade
Version history
3.3.0latest on npm
Audit
Dependencies
webpackrequiredPeer dependency; loader is designed for Webpack 5+
Agent activity
11 hits · last 30 days
node
10
OpenAI (training)
1
Resources
string-replace-loader — npm install string-replace-loader · libregistry