serlingpa
serlingpa

Reputation: 12700

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?

Upvotes: 3

Views: 3016

Answers (2)

frontendthug
frontendthug

Reputation: 21

Since webpack uses modules, but jQuery needs to be global a workaround is needed.

You need to use expose loader

require("expose?$!jquery");

or

module: {
 loaders: [
  { test: require.resolve("jquery"), loader: "expose?$!expose?jQuery" },
 ]
}

For more detailed explanation see: https://65535th.com/jquery-plugins-and-webpack/

Upvotes: 2

SlashmanX
SlashmanX

Reputation: 2611

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)

Upvotes: 2

Related Questions