Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery and webpack: how to require

I know there are several similar questions on stackoverflow regarding this issue but I have followed a lot of them and they have only partially helped.

I am trying to require jQuery in my Angular app using webpack. However, the browser complains that there is no jQuery ($) to be found:

Uncaught TypeError: Cannot read property 'fn' of undefined

and the code that raises this error:

$.fn[_iCheck] = function(options, fire) { ... }

I have checked and $ is undefined at this point in the code. I think I have setup jQuery correctly in webpack. I have npm installed it and I am requiring it like this:

var jQuery = require('jquery');

yet I still get the error complaining that $ is undefined.

Here is my webpack.config.js. I have removed the modifications I made having read some of the other posts on stackoverflow:

var sliceArgs = Function.prototype.call.bind(Array.prototype.slice);
var toString = Function.prototype.call.bind(Object.prototype.toString);
var path = require('path');
var webpack = require('webpack');

module.exports = {
    devtool: 'source-map',
    debug: true,
    devServer: {
        historyApiFallback: true,
        contentBase: 'src/public',
        publicPath: '/__build__'
    },
    entry: {
        'app': './src/app/bootstrap',
        'vendor': './src/app/vendor.ts'
    },
    output: {
        path: root('__build__'),
        filename: '[name].js',
        sourceMapFilename: '[name].js.map',
        chunkFilename: '[id].chunk.js'
    },
    resolve: {
        extensions: ['', '.ts', '.js', '.json', '.css', '.html']
        // ,
        // alias: { jquery: 'jquery/src/jquery'}
    },
    module: {
        loaders: [
            { test: /\.json$/, loader: 'json' },
            { test: /\.css$/, loader: 'raw' },
            { test: /\.html$/, loader: 'html' },
            {
                test: /\.ts$/,
                loader: 'ts',
                exclude: [/\.spec\.ts$/, /\.e2e\.ts$/, /node_modules/]
            },
            {
                test: /\.scss$/,
                loaders: ['style', 'css?sourceMap', 'sass?sourceMap']
            },
            {
                test: /\.(woff|woff2)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                loader: "url-loader?limit=10000&minetype=application/font-woff"
            },
            {
                test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                loader: "file-loader"
            }
        ]
    },
    plugins: [
        new webpack.optimize.CommonsChunkPlugin({ name: 'vendor', filename: 'vendor.bundle.js' }),
        new webpack.ProvidePlugin({ $: "jquery", jQuery: "jquery" })
    ]
};

function root(args) {
    args = sliceArgs(arguments, 0);
    return path.join.apply(path, [__dirname].concat(args));
}

function rootNode(args) {
    args = sliceArgs(arguments, 0);
    return root.apply(path, ['node_modules'].concat(args));
}

Can anyone help?

like image 934
serlingpa Avatar asked Dec 13 '25 14:12

serlingpa


1 Answers

Replace your require statement with the following:

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

This sets the $ (and jQuery) properties on the window object correctly (similar to using the script tags in basic JS)

like image 178
SlashmanX Avatar answered Dec 15 '25 04:12

SlashmanX



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!