Critical dependency: require function is used in a way in which dependencies cannot be statically extracted

刘超 12天前 ⋅ 111 阅读   编辑

一、描述

  执行npm run build报如下错误

WARNING in ./node_modules/yargs/index.js 13:39-46
Critical dependency: require function is used in a way in which dependencies cannot be statically extracted
 @ ./node_modules/@umijs/utils/lib/index.js
 @ ./node_modules/umi/lib/cjs.js
 @ ./node_modules/umi/index.js
 @ ./app/containers/Index/index.tsx
 @ ./app/routes.ts
 @ ./app/app.tsx
 @ multi E:/Workspace/Project/node_modules/react-app-polyfill/ie11.js ./app/app.tsx

二、分析

  1、查看./node_modules/yargs/index.js文件, 该文件中的确出现了require这个词,如下

 

  2、在package.json没有yargs,但node_modules是有yargs,这里就直接删除node_modules/yargs,再次尝试还是不行

  3、在package.json中将webpack@^4.41.6改为webpack@4.44.1,还是不行

  4、在Webpack的module中添加如下配置

module: {
    // ...
    // 解决Critical dependency: require function is used in a way in which dependencies cannot be statically extracted的问题
    unknownContextCritical : false,
    ...
}

  再次执行npm run build,就正常

三、解决方法

 1、在Webpack的module中添加如下配置

module: {
    // ...
    // 解决Critical dependency: require function is used in a way in which dependencies cannot be statically extracted的问题
    unknownContextCritical : false,
    ...
}

  2、执行npm run build

四、参考文章
  1、https://blog.csdn.net/DiegoBrother/article/details/102742550
  2、https://github.com/webpack/webpack/issues/2675


注意:本文归作者所有,未经作者允许,不得转载

全部评论: 0

    我有话说: