繁体   English   中英

为什么 Jquery $ 在 webpack 中显示为 undefined?

[英]Why is Jquery $ showing undefined in webpack?

我在 src 文件夹中的 index.js 文件中有以下代码:

 import './styles/main.scss'; import $ from 'jquery'; //Smooth scroll $(".navbar a").on("click", function (e) { e.preventDefault(); if (this.hash.== "") { const hash = this;hash, $("html. body"):animate( { scrollTop. $(hash).offset(),top, }; 800 ); } });
但是,当我构建项目时出现此错误:

 ERROR in./src/index.js Module not found: Error: Can't resolve 'jquery' in '[url]\src' @./src/index.js 2:0-23 4:0-1 9:4-5 10:17-18 @./node_modules/html-webpack-plugin/lib/loader.js../src/index.html

我无法弄清楚是什么导致了这个问题。 我遵循了 webpack 有关配置外部设备的文档,但我仍然遇到此错误。 如果您查看我的配置文件,您会看到我配置了外部设备。 这是我的 webpack 配置文件:

 const path = require("path"); const HtmLWebPackPlugin = require("html-webpack-plugin"); const MiniCSSExtractPlugin = require("mini-css-extract-plugin"); module.exports = { module: { rules: [ { test: /\.js$/, exclude: /node_modules/, use: { loader: "babel-loader", }, }, { test: /\.html$/, use: [ { loader: "html-loader", options: { minimize: true }, }, ], }, { test: /\.(png|svg|jpg|gif|jpeg)$/, use: ["file-loader"], }, { test: /\.scss$/, use: ["css-loader", "sass-loader"], }, ], }, plugins: [ new HtmLWebPackPlugin({ template: "./src/index.html", filename: "./index.html", }), new MiniCSSExtractPlugin({ filename: "[name].css", chunkFilename: "[id].css", }), ], externals: { jquery: "jQuery", }, };

包.json

替换import $ from 'jquery';

jQuery = $ = window.jQuery = require('jquery');

暂无
暂无

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

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