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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM