Webpack stepping on the pit cannot resolve jquery and webpack-cli

Recently when learning Vue, when using webpack, there was an error, it may be a version 3 and 4 problem

webpack-dev-server

After installation webpack-dev-server, you need package.jsonto scriptsadd code in"dev": "webpack-dev-server"

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "webpack-dev-server"
  },

Then the error was reported at this time ~~~ The CLI moved into a separate package: webpack-cli

A lot, I did n’t understand it at the beginning, and I did n’t understand it. Finally, I found it simple, but I just installed webpack-cli.

无法解析jquery Module not found: Error: Can't resolve 'jquery' in 'F:\Study\webpack-study\src'

Seeing this is a bit confusing, I have installed it, and then it can be run, and the result is this error.

Since jquery.placholder.min.jsUMD is used as a loading strategy, it recognizes that it is through require-required and tries to require the use of jQuery in the same way:

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

Webpack looks at require("jquery")and tries to bundle the jQuery library (which does not exist in node_modules) The
solution is to add jQuery as an external to 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 module, Webpack does not bundle the module, but uses global variables.

Reference: webpack Can't resolve 'jquery' .

Guess you like

Origin www.cnblogs.com/jellydong/p/12716467.html