webpack rejects a rule that mixes resource with test, include or exclude
Error: Rule can only have one resource source (provided resource and test + include + exclude)Short answer
In a webpack rule, test, include, exclude and resource all match the same thing: the resolved path of the file. The docs say you cannot supply resource together with any of the other three. Pick one style for the rule, or move the extra conditions inside one condition object.
Fix it fast
1Find out which cause you have
Find the rule the message points to. Look for a rule object that has resource next to test, include or exclude, often created by a helper or by merging two configs.
grep -n -B2 -A8 "resource" webpack.config.js2Match what you see, then jump to its fix
- 1
The provided list in the message shows resource and testresource and test sit in the same ruleFix - 2
The provided list shows resource together with include or excluderesource is combined with include or exclude (for example node_modules)Fix - 3
Your own file uses test, but a helper, preset or merged config adds resource to the same ruleTwo configs were merged and each side used a different styleFix
- 1
What does the error look like?
Error: Rule can only have one resource source (provided resource and test + include + exclude)webpack fails while reading module.rules from webpack.config.js
Cause 1 of 3
resource and test sit in the same rule
1Check
The docs say that if you supply Rule.test you cannot also supply Rule.resource. Both are matched against the resource path, so only one may be used.
grep -n -A6 "resource:" webpack.config.js2Fix
Keep test for the file pattern and drop resource, or express the pattern inside resource and drop test.
// before
{ resource: /\.js$/, test: /\.js$/, loader: 'babel-loader' }
// after
{ test: /\.js$/, loader: 'babel-loader' }3Verify
Run the build again; the rule error is gone.
npx webpack --config webpack.config.jsConfirmed by webpack.js.org
Cause 2 of 3
resource is combined with include or exclude (for example node_modules)
1Check
The same restriction is documented for include and for exclude. A config that adds exclude: /node_modules/ to a rule that already uses resource breaks for this reason.
grep -n -B3 -A3 "node_modules" webpack.config.js2Fix
Use test with include or exclude, or fold everything into one resource condition object with and and not.
{
resource: {
and: [/\.js$/],
not: [/node_modules/],
},
loader: 'babel-loader',
}3Verify
Build again and confirm babel-loader still skips node_modules.
npx webpack --config webpack.config.jsConfirmed by webpack.js.org
Cause 3 of 3
Two configs were merged and each side used a different style
1Check
Print the final rules after merging and look at the one that has both keys.
node -e "console.log(JSON.stringify(require('./webpack.config.js').module.rules, (k, v) => v instanceof RegExp ? String(v) : v, 2))"2Fix
Normalise the rules before they are returned: for every rule that has resource, move the pattern to test or delete the duplicate key.
config.module.rules = config.module.rules.map((rule) => {
if (rule.resource && rule.test) {
const { resource, ...rest } = rule;
return rest;
}
return rule;
});3Verify
The printed rules show one resource style per rule, and the build runs.
npx webpack --config webpack.config.jsConfirmed by webpack.js.org
How do I stop it coming back?
- Use test, include and exclude for normal rules; reach for resource only when you need an object condition.
- After merging configs, print the rules once and read them.
- Read the Rule conditions section of the webpack docs when a rule needs more than one pattern.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Web apps
tsconfig rejects allowImportingTsExtensions
An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly
allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.
2 causes, each with a fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fix