Webpack - Critical dependency: the request of a dependency is an expression

WebpackRequestAjv

Webpack Problem Overview


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 or expressions in a require call (such as require('' + 'nodent') in these lines of 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

Webpack Solutions


Solution 1 - Webpack

Solved with npm install [email protected] --save

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

Solution 2 - Webpack

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'), {} )

Solution 3 - Webpack

This warning can be linked to packages injections in (dependancies or devDependencies).

If the problem suddenly appears, check the last modification in your package.json.

Consider removing package-lock.json if you plan to relaunch an npm install.

Solution 4 - Webpack

I got this in Angular when I imported EventEmitter from 'protractor' by accident. I blame my IDE for even suggesting it!

It should be imported from core:

import { EventEmitter } from '@angular/core';

Solution 5 - Webpack

I had this same warnnig working with with typeorm and nextjs. I silenced it by doing using the code from here

const FilterWarningsPlugin = require('webpack-filter-warnings-plugin');

module.exports = {
    ...
    plugins: [
        //ignore the drivers you don't want. This is the complete list of all drivers -- remove the suppressions for drivers you want to use.
        new FilterWarningsPlugin({
            exclude: [/mongodb/, /mssql/, /mysql/, /mysql2/, /oracledb/, /pg/, /pg-native/, /pg-query-stream/, /react-native-sqlite-storage/, /redis/, /sqlite3/, /sql.js/, /typeorm-aurora-data-api-driver/]
        })
    ]
};

I added a regex like this to the exclude array above.

/Critical dependency/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJodiugView Question on Stackoverflow
Solution 1 - WebpackJodiugView Answer on Stackoverflow
Solution 2 - WebpackDhirendraNkView Answer on Stackoverflow
Solution 3 - WebpackL Y E S - C H I O U K HView Answer on Stackoverflow
Solution 4 - Webpackdanday74View Answer on Stackoverflow
Solution 5 - WebpackMayowa DanielView Answer on Stackoverflow