Webpack打包时警告 - Critical dependency: the request of a dependency is an expression

关于解决 [Webpack] Critical dependency: the request of a dependency is an expression

-------------------------------------------------------------------------------------------------

就遇到的情况和查阅相关资料来看,这个问题一般出现在 编译用于node环境时:

module.exports = {
  target: 'node',
  ...
}

使用 target: 'node' 意为加载核心模块时,不打包? node_modules 里面的模块。

但实际上光靠target指定为node是不够的,还需要借助一项外部配置 webpack-node-externals, 它可以帮助我们在打包时忽略 node_modules ~

如何使用:

  1. 安装(-S/-D都可,看个人情况)

npm i webpack-node-externals
// or
yarn add webpack-node-externals

  2. 使用

const nodeExternals = require('webpack-node-externals')

const config = function() {
  return {
    ...
    externals: [
      nodeExternals()
    ],
    ...
  
  }
}

module.exports = config

 

end

转载于:https://www.cnblogs.com/fanqshun/p/10073493.html

你可能感兴趣的:(Webpack打包时警告 - Critical dependency: the request of a dependency is an expression)