Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ProvidePlugin not working with jquery - webpack

I am trying to access jquery in my application according to information on webpack website you are able to create global variable using webpack https://webpack.js.org/plugins/provide-plugin/

here is my webpack set up

module.exports = {
    entry: {
        index: "./scripts/index.js",
        vendorJS: ["jquery", "jquery-validation", "jquery-validation-unobtrusive"]
    }
    ,
    output: {
        path: path.join(__dirname, '/wwwroot/'),
        filename: '[name].js'
    },
    resolve: {
        extensions: ['.js', '.jsx', '.css', '.scss', '.sass']
    },
    module: {
        rules: [
            {
                test: /\.scss$/i,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: ['css-loader', 'sass-loader']
                })
            },
            {
                test: /\.css$/i,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: ['css-loader']
                })
            },
            {
                test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                use: ['url-loader?limit=10000&mimetype=application/font-woff&name=/fonts/[name].[ext]'],
            },
            {
                test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                use: ['file-loader?name=/fonts/[name].[ext]'],
            }
        ]
    },
    plugins: [
        new CleanWebpackPlugin(['./wwwroot']),
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery",
            "window.jQuery": "jquery"
        }),
        css
    ],

}

using thius set up i am unable to access my $ - jquery in console menu.

enter image description here

any idea why this is happening?

like image 975
kkdeveloper7 Avatar asked Aug 26 '17 20:08

kkdeveloper7


1 Answers

I know it's late, but for Googlers (or Duck Duck goers if you care about privacy), I had the same problem:

if I used $: 'jquery' It threw

Can't resolve 'jquery'

If I used

const jquery = require('jquery');
$: jquery

It threw

Can't import module "undefined"

This finally worked for me:

new webpack.ProvidePlugin({
    $: require.resolve('jquery'),
    jQuery: require.resolve('jquery')
})
like image 71
Genarito Avatar answered Oct 31 '22 18:10

Genarito