简体   繁体   English

Webpack 未找到模块:错误:无法解析“jquery”

[英]Webpack Module not found: Error: Can't resolve 'jquery'

When I run the 'webpack' command, I get this error:当我运行“webpack”命令时,出现此错误:

ERROR in./js/main.js Module not found: Error: Can't resolve 'jquery' in '...\js' @./js/main.js 3:0-16 4:0-23错误 in./js/main.js 未找到模块:错误:无法解析 '...\js' 中的 'jquery' @./js/main.js 3:0-16 4:0-23

In package.json I have:在 package.json 我有:

"devDependencies": {
   "handlebars": "^4.0.6",
   "handlebars-loader": "^1.4.0",
   "jquery": "^3.2.1",
   "path": "^0.12.7"
},

In webpack.config.js:在 webpack.config.js 中:

var path = require("path");

module.exports = {
  entry: "./js/main.js",
  output: {
    path: __dirname + "/js",
    filename: "scripts-bundled.js"
  },
  resolve: {
    modules: [
      path.join(__dirname, "js/helpers")
    ]
  },
  module: {
    loaders: [
      {test: /\.hbs$/, loader: "handlebars-loader"}
    ]
  }
};

And in main.js at the top of the file, I have:在文件顶部的 main.js 中,我有:

import $ from 'jquery';

I'm also using handlebars in main.js.我还在 main.js 中使用把手。 Could it be that handlebars or handlebars-loader is interfering with the jquery?可能是 handlebars 或 handlebars-loader 干扰了 jquery? I've used webpack and jquery without this issue before in another project where I didn't use handlebars, but maybe it has nothing to do with it.我以前在另一个我没有使用把手的项目中使用过 webpack 和 jquery 没有这个问题,但也许它与它无关。

Well in my case it was something about importing jquery instead of jQuery , it is a webpack config:好吧,就我而言,它是关于导入jquery而不是jQuery 的,它是一个 webpack 配置:

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

have a look at this: webpack Can't resolve 'jquery'看看这个: webpack Can't resolve 'jquery'

The handlebars has nothing to do with it.车把与它无关。 The problem is that you changed resolve.modules to [path.join(__dirname, "js/helpers")] .问题是,你改变resolve.modules[path.join(__dirname, "js/helpers")] So webpack will only look in js/helpers for any module, but jquery and other dependencies from npm are in node_modules .所以 webpack 只会在js/helpers查找任何模块,但来自 npm 的jquery和其他依赖项在node_modules The default value of resolve.modules is ["node_modules"] .的默认值resolve.modules["node_modules"] You also need to add node_modules to keep the regular module resolution.您还需要添加node_modules以保持常规模块分辨率。

resolve: {
  modules: [
    path.join(__dirname, "js/helpers"),
    "node_modules"
  ]
},

使用以下命令解决错误。
npm install --save jquery

If you are using React with Bootstrap and you're getting this error, it may be because you're using the lower version of Bootstrap.如果您在使用 React 和 Bootstrap 时遇到此错误,可能是因为您使用的是较低版本的 Bootstrap。 I solved it by upgrading the version to Bootstrap version 5.我通过将版本升级到 Bootstrap 版本 5 来解决它。

如果使用 mac OSX,npm v5.0.0 或 > 在你的项目文件夹终端上试试这个命令npm install jquery jquery-ui

Rails 6: yarn add jquery work for me Rails 6: yarn add jquery为我工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM