webpack "Critical dependency: the request of a dependency is an expression"
webpack found a require() whose argument is a runtime expression, not a static string, so it cannot know which module to bundle. It emits "Critical dependency" - harmless as a warning, but CI configured to fail on warnings turns it into a build failure.
What this error means
The build prints Critical dependency: the request of a dependency is an expression, pointing at a package (often express, a logger, or an ORM) that builds a require path dynamically. With --bail or warning-as-error CI, the build then fails.
WARNING in ./node_modules/express/lib/view.js 81:13-25
Critical dependency: the request of a dependency is an expression
@ ./node_modules/express/lib/application.js
@ ./src/server.jsCommon causes
Dynamic require in a dependency
A library uses require(variable) or require(./${name}). webpack cannot statically resolve it, so it warns that the request is an expression.
CI treats warnings as failures
A stats.warningsFilter-less build with --bail, or a CI step that greps for "WARNING", elevates the otherwise-benign message into a hard failure.
How to fix it
Suppress the known-benign warning
When the dynamic require is in a server-side dependency you do not actually bundle for the browser, mark it external or ignore it.
// webpack.config.js
module: {
// exprContextCritical: false // silences expression-context warnings
},
ignoreWarnings: [/the request of a dependency is an expression/],Externalize the offending package
- For Node targets, add the package to
externalsso webpack does not try to bundle its dynamic requires. - Set
target: 'node'for server bundles so built-in/dynamic modules are left to the runtime. - Do not fail CI on this specific warning if the dependency is intentionally dynamic.
How to prevent it
- Set
target: 'node'and useexternalsfor server-side bundles. - Scope CI warning-as-error gates so known-benign dynamic-require warnings do not fail the build.
- Avoid dynamic
require(expr)in your own code; use static imports.