webpack Can't resolve 'jquery'

后端 未结 1 597
别跟我提以往
别跟我提以往 2021-01-02 04:44

Hi I\'m a new to Webpack , currently I\'m adding this tool to my project, during bundling (webpack ...) jquery-dependent library I get an error like that:

相关标签:
1条回答
  • 2021-01-02 05:12

    Problem

    Since jquery.placholder.min.js is using UMD as its loading strategy, it's recognizing that it is required via a require - and tries to require jQuery in the same way:

    "object"==typeof module&&module.exports?require("jquery"):jQuery
    

    Webpack sees require("jquery") and tries to bundle the jQuery library (which does not exist in the node_modules).

    Solution

    The solution is to add jQuery as an external in your webpack.config.js:

    {
      ...
      externals: {
        // require("jquery") is external and available
        //  on the global var jQuery
        "jquery": "jQuery"
      }
    }
    

    When a module is marked as an external, Webpack knows not to bundle it, but instead use the global variable.

    0 讨论(0)
提交回复
热议问题