简体   繁体   English

webpack - ReferenceError: $ 未定义

[英]webpack - ReferenceError: $ is not defined

I've read many answers to this question but I can't get to resolve my issue so I come here for help...我已经阅读了这个问题的很多答案,但我无法解决我的问题,所以我来这里寻求帮助...

Here's my webpack conf file :这是我的 webpack conf 文件:

const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
// https://github.com/webpack-contrib/mini-css-extract-plugin
const webpack = require('webpack');
const jquery = require('jquery');

module.exports = {
    mode: 'development',
    entry: './assets/js/app.js',
    output: {
        path: path.resolve(__dirname, 'public/dist'),
        filename: 'bundle.js'
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [
                    {
                        loader: MiniCssExtractPlugin.loader,
                        options: {
                            publicPath: '../',
                            hmr: process.env.NODE_ENV === 'development',
                        },
                    },
                    'css-loader',
                ],
            },
            {
                test: /\.(png|jpg|gif)$/i,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            limit: 100000,
                        },
                    },
                    {
                        loader:"file-loader",
                        options:{
                            name:'[name].[ext]',
                            outputPath:'images/' //the images will be emited to dist/images/ folder
                        }
                    }
                ],
            },
            {
                // Exposes jQuery for use outside Webpack build
                test: require.resolve('jquery'),
                use: [{
                    loader: 'expose-loader',
                    options: 'jQuery'
                }, {
                    loader: 'expose-loader',
                    options: '$'
                }]
            }
        ]
    },
    plugins: [
        new MiniCssExtractPlugin({
            filename: '[name].css',
            chunkFilename: '[name]-[id].css',
            ignoreOrder: false,
        }),
        /* Use the ProvidePlugin constructor to inject jquery implicit globals */
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery",
            "window.jQuery": "jquery'",
            "window.$": "jquery"
        })
    ],
    /*resolve: {
        alias: {
            jquery: "jquery/src/jquery"
        }
    }*/
};

And my entry file app.js :我的入口文件 app.js :

// Import CSS
import 'bootstrap/dist/css/bootstrap.css';
import '@fortawesome/fontawesome-free/js/all';
import '../css/style.css';

// Import JS
import $ from 'jquery';
window.$ = jQuery;
window.jQuery = jQuery;

import * as JSZip from 'jszip'; //export xlsx

import 'bootstrap';

//require('webpack-jquery-ui');
//require('webpack-jquery-ui/css');

I keep on trying different solution but still when I use jQuery in my code I get :我一直在尝试不同的解决方案,但当我在代码中使用 jQuery 时,我仍然得到:

ReferenceError: $ is not defined参考错误:$ 未定义

Could you please help me out ?你能帮我一下吗?

One solution to add the jquery library as external.将 jquery 库添加为外部的一种解决方案。 The second solution is to add jquery to the bundle.第二种解决方案是将 jquery 添加到包中。

1. External webpack.js 1. 外部webpack.js

externals: {   
  jquery: 'jQuery'
}
plugins: [   new webpack.ProvidePlugin({      
  $: 'jquery',
  jQuery: 'jquery',
  'window.jQuery': 'jquery'    
})]

adding jquery to html将 jquery 添加到 html

<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous">

adding jquery in js在js中添加jquery

import $ from 'jquery';

const test = $('h1').text();
console.log(`from jquery: $ {test}`);

2. The second solution, adding jquery to the bundle First you need to install jquery locally. 2.第二种方案,将jquery加入bundle首先需要在本地安装jquery。

yarn add -D jquery

adding jquery in js在js中添加jquery

import $ from 'jquery';

const test = $('h1').text();
console.log(`from jquery: $ {test}`);

This can be optimized and split using splitChunks but that's a story for another entry;)这可以使用 splitChunks 进行优化和拆分,但这是另一个条目的故事;)

I added an example of external jquery use我添加了一个外部jquery使用的例子

Look at this in your code:在你的代码中看看这个:

import $ from 'jquery';
window.$ = jQuery;
window.jQuery = jQuery;

Think a second.想一想。

. .

. .

. .

. .

. .

. .

. .

. .

. .

. .

. .

. .

Notice you never defined jQuery .请注意,您从未定义jQuery Fix it.修理它。

import $ from 'jquery';
window.$ = $;
window.jQuery = $;

OK the mistake was stupid, app.js was called at the end of the layout and I was trying to use jQuery in the view.好吧,这个错误很愚蠢,app.js 在布局结束时被调用,我试图在视图中使用 jQuery。

So make sure that you check the order in which your broken code and the include which contains jquery is called.因此,请确保检查损坏的代码和包含 jquery 的包含的调用顺序。

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

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