Skip to content
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

"$ is undefined" with .autoProvidejQuery() and jQuery plugin #52

Closed
heitorvrb opened this issue Jun 22, 2017 · 4 comments
Closed

"$ is undefined" with .autoProvidejQuery() and jQuery plugin #52

heitorvrb opened this issue Jun 22, 2017 · 4 comments

Comments

@heitorvrb
Copy link

I'm trying to use the "foundation-datepicker" package and getting "TypeError: $ is undefined".

app/Resources/js/app.js:

require('foundation-sites');
require('foundation-datepicker/js/foundation-datepicker.js');
[...]

.autoProvidejQuery() is enabled in webpack.config.js. .autoProvidejQuery() works with other jQuery-dependant packages, but when I add this one, it gives the error.

Could it be something in the package? According to the browser profiler, the guilty lines in foundation-datepicker.js are:

! function($) {

[...]

$.fn.fdatepicker = function(option) {

[...]

What should I do? Sorry if this is not very webpack-encore specific.

@kcivey
Copy link

kcivey commented Jun 22, 2017

The autoProvideVariables() method is not working for me either. I had to make a separate file containing lines like

window.$ = window.jQuery = require('jquery');
window.moment = require('moment');
window._ = require('underscore');

and include it in the entry.

@weaverryan
Copy link
Member

Hi guys!

I know the problem for foundation-datepicker (at least). Add this:

Encore
    // ...
    autoProvideVariables({
        'window.jQuery': 'jquery'
    })

There was a PR to add this to autoProvidejQuery (#10), but it has some unfortunate side effects (#32). This should fix your issue. Internally, we'll debate the best "default" option.

@kcivey You may have been having the same issue... but I would need a bit more code and the error details to know for sure :).

@heitorvrb
Copy link
Author

It works. Thanks for your help

weaverryan added a commit that referenced this issue Jun 28, 2017
This PR was merged into the master branch.

Discussion
----------

Re-adding window.jQuery to autoProvidejQuery

By adding `window.jQuery` to `autoProvidejQuery()`, it caused problems by making `window.jQuery = require('jquery')` not work (#32). But, *not* having it causes other obvious problems (see #52).

This re-adds `window.jQuery` back. As a fix, we would document exposing jQuery as a global variable by doing this:

```js
global.$ = global.jQuery = require('jquery');
```

ping @alOneh

Commits
-------

9653f1e re-adding window.jQuery to autoProvidejQuery
@pmpr
Copy link

pmpr commented Oct 28, 2017

Node: 8.7.0
Yarn: 1.2.1
Encore: 0.16.0

same problem here with Bootstrap 3.3.7 and on page, arbitrary jQuery stuff.
BS is working as normal, but other jQuery stuff on the page say:

Uncaught ReferenceError: $ is not defined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants