{"id":20510,"library":"replace-in-file-webpack-plugin","title":"ReplaceInFileWebpackPlugin","description":"A webpack plugin that replaces strings in files after the compilation is done. Version 1.0.6 (latest) provides a simple API to define replacement rules for files in specified directories, supporting both string and regex patterns with string or function replacements. It is useful for modifying output files (HTML, CSS, JS) that are not processed by loaders. Compared to alternatives like string-replace-webpack-plugin, it offers a cleaner configuration with an array of rule sets and supports multiple file matching via `files` or `test` patterns.","status":"active","version":"1.0.6","language":"javascript","source_language":"en","source_url":"https://github.com/oyslin/replace-in-file-webpack-plugin","tags":["javascript","webpack","plugin","replace","file"],"install":[{"cmd":"npm install replace-in-file-webpack-plugin","lang":"bash","label":"npm"},{"cmd":"yarn add replace-in-file-webpack-plugin","lang":"bash","label":"yarn"},{"cmd":"pnpm add replace-in-file-webpack-plugin","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency; plugin hooks into webpack's compiler lifecycle.","package":"webpack","optional":false}],"imports":[{"note":"Default export; named import will fail.","wrong":"const { ReplaceInFileWebpackPlugin } = require('replace-in-file-webpack-plugin')","symbol":"ReplaceInFileWebpackPlugin","correct":"import ReplaceInFileWebpackPlugin from 'replace-in-file-webpack-plugin'"},{"note":"CommonJS require works without .default.","wrong":"const ReplaceInFileWebpackPlugin = require('replace-in-file-webpack-plugin').default","symbol":"ReplaceInFileWebpackPlugin","correct":"const ReplaceInFileWebpackPlugin = require('replace-in-file-webpack-plugin')"}],"quickstart":{"code":"// webpack.config.js\nconst path = require('path');\nconst ReplaceInFileWebpackPlugin = require('replace-in-file-webpack-plugin');\n\nmodule.exports = {\n  entry: './src/index.js',\n  output: {\n    path: path.resolve(__dirname, 'dist'),\n    filename: 'bundle.js',\n  },\n  plugins: [\n    new ReplaceInFileWebpackPlugin([\n      {\n        dir: 'dist',\n        files: ['index.html'],\n        rules: [\n          { search: '{{VERSION}}', replace: '1.0.0' },\n          { search: /@year/g, replace: '2024' },\n        ],\n      },\n    ]),\n  ],\n};","lang":"javascript","description":"Shows how to configure the plugin in webpack.config.js to replace placeholders in output files."},"warnings":[{"fix":"Ensure replace functions always return a string.","message":"The `replace` option in rules can be a string or function, but if the function returns a non-string value (e.g., undefined), it will be converted to the string 'undefined'.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Provide either `files` (array of filenames) or `test` (regex or array of regexes) in each config object.","message":"If both `files` and `test` are omitted, no files will be processed. The plugin requires at least one of these to specify target files.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Use `test` with regex if you need pattern matching, or list all filenames explicitly.","message":"The plugin does not support glob patterns in `files`; only exact filenames are matched.","severity":"gotcha","affected_versions":">=0.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Use `const ReplaceInFileWebpackPlugin = require('replace-in-file-webpack-plugin');` or `import ReplaceInFileWebpackPlugin from 'replace-in-file-webpack-plugin';`","cause":"Importing the plugin incorrectly, e.g., destructuring a named export that doesn't exist.","error":"TypeError: ReplaceInFileWebpackPlugin is not a constructor"},{"fix":"Run `npm install replace-in-file-webpack-plugin --save-dev`","cause":"Package not installed or missing from node_modules.","error":"Error: Cannot find module 'replace-in-file-webpack-plugin'"},{"fix":"Wrap your config in an array: `new ReplaceInFileWebpackPlugin([{ ... }])`","cause":"Passing a single config object instead of an array.","error":"Error: The provided value must be an array of configurations"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}