Webpack - Critical dependency: the request of a de

2019-03-17 03:53发布

I am getting three warning messages when importing request in a barebone webpack project. A minimal example to reproduce the bug is available on GitHub (run npm install and npm start).

Critical dependency: the request of a dependency is an expression

How can I get rid of this warning?


More information:

Webpack tries to resolve require calls statically to make a minimal bundle. When a library uses variables in a require call (such as these lines in ajv), Webpack cannot resolve them statically and imports the entire package.

My rationale is that this dynamic import is not desirable in production, and code is best kept warning-free. That means I want any solution that resolves the problem. E.g.:

  1. Manually configure webpack to import the required libraries and prevent the warnings from occurring.
  2. Adding a hack.js file to my project that overrides the require calls in some way.
  3. Upgrading my libraries. ajv-5.0.1-beta.3 has a fix that silences the warnings. However, if I want to use it, I have to wait until it is released, and then until har-validator and request release subsequent updates. If there is a way to force har-validator to use the beta version of ajv, that would solve my problem.
  4. Other

2条回答
贪生不怕死
2楼-- · 2019-03-17 04:12

Solved with npm install request@2.79.0 --save

According to the authors of ajv, the issue will likely be resolved in the latest version of request in a few weeks' time.

查看更多
爷、活的狠高调
3楼-- · 2019-03-17 04:14

Replace this

new webpack.ContextReplacementPlugin(
        /angular(\\|\/)core(\\|\/)@angular/,
        helpers.root('./src'), // location of your src
        {} // a map of your routes
    ),

with this-

new webpack.ContextReplacementPlugin( /(.+)?angular(\\|\/)core(.+)?/, root('./src'), {} )
查看更多
登录 后发表回答