Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

webpack rejects a rule that mixes resource with test, include or exclude

ErrorError: 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.

  • Applies to webpack 5, Node.js 22, Vue CLI style configs
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.js
  2. 2Match what you see, then jump to its fix

    1. 1The provided list in the message shows resource and testresource and test sit in the same ruleFix
    2. 2The provided list shows resource together with include or excluderesource is combined with include or exclude (for example node_modules)Fix
    3. 3Your 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

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.js

2Fix

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.js

Confirmed by webpack.js.org

Did this fix it?

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.js

2Fix

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.js

Confirmed by webpack.js.org

Did this fix it?

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.js

Confirmed by webpack.js.org

Did this fix it?

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.