New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'Uncaught ReferenceError: jQuery is not defined' when using webpack #322

Open
mark-norgate opened this Issue Feb 19, 2016 · 5 comments

Comments

Projects
None yet
6 participants
@mark-norgate

mark-norgate commented Feb 19, 2016

I am writing an app using Angular 2, webpack and I am using the icheck front-end library.

I am requiring them in my component thusly:

var jQuery: any = require("jquery");
require('../../../js/jquery.icheck.min.js');
require('../../../css/checkbox/orange.css');

I have also tried like this:

require("jquery");
require('../../../js/jquery.icheck.min.js');
require('../../../css/checkbox/orange.css');

This builds successfully using the webpack server and npm, but I get an error in the browser:

Uncaught ReferenceError: jQuery is not defined (jquery.min.icheck.js:11)

I have used this plugin before in an Angular 2 component but I never had any problems like this. I have compared the current app with the working app and I can't see any differences; it should work!

I am really up against it now and have to get this prepared for Monday. Anyone have any problems like this?

@raulferras

This comment has been minimized.

raulferras commented Apr 19, 2016

Try this:

var $ = require('jquery');
window.jQuery = $;
require('../../../js/jquery.icheck.min.js');
require('../../../css/checkbox/orange.css');

iCheck is not ready neither for AMD nor CommonJS module system so it expects jQuery to be defined in the window object.
@cnlevy

This comment has been minimized.

cnlevy commented May 5, 2016

I had the same problem with bootstrap-switch
I followed @raulferras advice which did solve my problem
Then i added the following in webpack.config.js

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

and it now works without workarounds

@gaurav123337

This comment has been minimized.

gaurav123337 commented May 21, 2016

Thanks a lot. 👍 raulferras

@farin

This comment has been minimized.

farin commented Aug 14, 2016

FYI, for me, provide plugin plugin works only without window.

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

Anyway, Thx 👍

@maqduni

This comment has been minimized.

maqduni commented Mar 8, 2017

You could also use expose-loader. The example below is written for webpack 2,

module: {
    rules: [
      { test: /[\/]jquery\.js$/, use: 'expose-loader?$!expose?jQuery' }
    ]
  },

dgdavid added a commit to dgdavid/rails4-webpack2-sample-app that referenced this issue Apr 2, 2018

Expose jQuery to global object
Because in many scenarios, such as when a _.js.erb_ file is rendered, is
needed to have it available globally, in order to avoid the `$ is not
defined` error.

More info:
fronteed/icheck#322 (comment)
https://github.com/webpack-contrib/expose-loader#usage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment