Uncaught ReferenceError: $ is not defined Webpack and embedded script

空扰寡人 提交于 2019-12-05 17:43:14

One solution it seems to work is to expose the jQuery module outside of the webpack bundle.

module: {
    rules: [
        {
            test: /\.tsx?$/,
            loader: 'ts-loader',
            exclude: /node_modules/,
        },
        {
            test: /\.css$/,
            loader: ExtractTextPlugin.extract({
                fallback: 'style-loader',
                use: 'css-loader'
            })
        },
        {
            // Exposes jQuery for use outside Webpack build
            test: require.resolve('jquery'),
            use: [{
                loader: 'expose-loader',
                options: 'jQuery'
            }, {
                loader: 'expose-loader',
                options: '$'
            }]
        }
    ]
},

Solution found here: Managing jQuery plugin dependency in webpack @harlemsquirrel

attach jquery.js file before JS Bundle

Based on the long discussion here, it looks like you need to resolve jquery module with a alias as $ or jQuery rather than defining it as a plugin.

here is the webpack config for that:

 resolve : {
    alias: {
        jquery: "./vendor/jquery/jquery.js",
    }
},

WebPack documentation page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!