Webpack Config Finder Utility
find-webpack is a utility library designed to programmatically locate and modify Webpack configuration settings, particularly useful for projects built with `react-scripts` (Create React App) or custom Webpack setups. As of its latest stable version, 2.2.1 (released January 2021), it provides functions to extract Webpack options, including handling the nuances of `react-scripts`'s hidden configurations. It also offers specific utilities for integrating with tools like Cypress, enabling cleanup of optimization plugins and dynamic addition of folders to Babel's transpilation scope for testing purposes. While its release cadence has been irregular since 2021, it was actively maintained with features and bug fixes throughout 2020, differentiating itself by simplifying complex Webpack config access and modification for specific ecosystem integrations.
Common errors
-
TypeError: Cannot read property 'includes' of undefined
cause This error often occurs in versions before 2.2.1 when `cleanForCypress` is called with `addFolderToTranspile` and the internal Babel presets array is undefined.fixUpgrade `find-webpack` to version `2.2.1` or later. Alternatively, ensure the Babel configuration passed to `cleanForCypress` explicitly defines presets if you're manipulating them. -
Webpack config is undefined or null when calling getWebpackOptions()
cause When using `find-webpack` to detect a `react-scripts` configuration, it expects a `package.json` file to be present in the current working directory to properly initialize `react-scripts`'s environment.fixEnsure that the script calling `getWebpackOptions()` is executed from the root directory of your project, where the `package.json` file resides. -
Incorrect environment variables (NODE_ENV, BABEL_ENV) detected after loading webpack config
cause The `tryLoadingWebpackConfig` and `getWebpackOptions` functions in `find-webpack` may internally set `process.env.NODE_ENV` and `process.env.BABEL_ENV` to 'development' or 'test' (for v2.0.0+) during config loading, which can persist.fixBe mindful of this side effect. If you need specific environment variables, set them before calling `find-webpack` functions or reset them immediately afterwards.
Warnings
- breaking When loading webpack configurations, `find-webpack` version 2.0.0 introduced a breaking change that requires `process.env.NODE_ENV` to be set to 'test' for proper loading of environment variables and plugins, particularly for `react-scripts` based projects.
- gotcha The `getWebpackOptions` function, when used with `react-scripts` projects, expects a `package.json` file to be present in the current working directory (`process.cwd()`). If `package.json` is missing, `react-scripts` will not load its configuration, and `find-webpack` will fail to retrieve it.
- gotcha The `tryLoadingWebpackConfig` function and implicitly `getWebpackOptions` can set `process.env.BABEL_ENV` and `process.env.NODE_ENV` to 'development' during the loading process and leave them in that state. This could unintentionally affect other parts of your application or build process that rely on these environment variables.
- gotcha A bug in earlier versions (fixed in v2.2.1) could lead to a `TypeError: Cannot read property 'includes' of undefined` when using the `addFolderToTranspile` option within `cleanForCypress`, if `options.babelPresets` was undefined.
Install
-
npm install find-webpack -
yarn add find-webpack -
pnpm add find-webpack
Imports
- getWebpackOptions
import findWebpack from 'find-webpack'; const config = findWebpack.getWebpackOptions();
import { getWebpackOptions } from 'find-webpack'; // Or for CJS: const { getWebpackOptions } = require('find-webpack'); - tryLoadingWebpackConfig
const config = require('find-webpack').tryLoadingWebpackConfig();import { tryLoadingWebpackConfig } from 'find-webpack'; // Or for CJS: const { tryLoadingWebpackConfig } = require('find-webpack'); - cleanForCypress
findWebpack.cleanForCypress(opts, config);
import { cleanForCypress } from 'find-webpack'; // Or for CJS: const { cleanForCypress } = require('find-webpack');
Quickstart
import { getWebpackOptions, cleanForCypress } from 'find-webpack';
import path from 'path';
const projectRoot = process.cwd();
console.log('Attempting to find Webpack options...');
let webpackConfig = getWebpackOptions();
if (webpackConfig) {
console.log('Webpack config found. Initial entry points:', webpackConfig.entry);
// Example of cleaning the config for Cypress with coverage and custom folder transpilation
const cypressComponentFolder = path.join(projectRoot, 'cypress', 'component');
const cypressFixturesFolder = path.join(projectRoot, 'cypress', 'fixtures');
const cleanOptions = {
reactScripts: true, // Specific cleanup for react-scripts projects
coverage: true, // Add istanbul babel plugin for code coverage
addFolderToTranspile: [cypressComponentFolder, cypressFixturesFolder],
looseModules: true // Enable @babel/plugin-transform-modules-commonjs
};
cleanForCypress(cleanOptions, webpackConfig);
console.log('Webpack config cleaned for Cypress. Modified entry points:', webpackConfig.entry);
// console.log('Full modified config:', JSON.stringify(webpackConfig, null, 2));
} else {
console.warn('Could not find Webpack options. Ensure package.json is in CWD or pass a config path.');
}