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 not defined (with webpack) #272

Open
maejml opened this issue Jan 8, 2020 · 3 comments
Open

$ is not defined (with webpack) #272

maejml opened this issue Jan 8, 2020 · 3 comments

Comments

@maejml
Copy link

maejml commented Jan 8, 2020

Got this error when I import sticky-kit with webpack :

Uncaught TypeError: $ is not a function at Object.<anonymous> (sticky-kit.js:9) at Object../node_modules/sticky-kit/dist/sticky-kit.js (sticky-kit.js:326) at __webpack_require__ (bootstrap:19) ...

My import line import 'sticky-kit/dist/sticky-kit'; is working.
jQuery is working too (debugged with a console log), I have the right lines in environment.js and config etc.

In the config, I added this rule as seen in another question but it didn't work :
{ test: /\.js$/, include: [ path.join(__dirname, 'node_modules', 'sticky-kit') ], use: [{ loader: 'imports-loader?jQuery=jquery,$=jquery,window=>global&window.jQuery=jquery,this=>window,define=>false' }] }

What am I not doing right ?

@dimasi
Copy link

dimasi commented Apr 4, 2020

+1

@dimasi
Copy link

dimasi commented Apr 4, 2020

Got this error when I import sticky-kit with webpack :

Uncaught TypeError: $ is not a function at Object.<anonymous> (sticky-kit.js:9) at Object../node_modules/sticky-kit/dist/sticky-kit.js (sticky-kit.js:326) at __webpack_require__ (bootstrap:19) ...

My import line import 'sticky-kit/dist/sticky-kit'; is working.
jQuery is working too (debugged with a console log), I have the right lines in environment.js and config etc.

In the config, I added this rule as seen in another question but it didn't work :
{ test: /\.js$/, include: [ path.join(__dirname, 'node_modules', 'sticky-kit') ], use: [{ loader: 'imports-loader?jQuery=jquery,$=jquery,window=>global&window.jQuery=jquery,this=>window,define=>false' }] }

What am I not doing right ?

Just add

window.jQuery = $;
window.$ = $;

in your common component.

@adriaandotcom
Copy link

adriaandotcom commented May 15, 2020

I solved this by adding window.jQuery in ProvidePlugin:

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

And in the file where I used $("#sidebar").stick_in_parent({}) I added:

import $ from 'jquery'
import 'sticky-kit/dist/sticky-kit'

Make sure you don't link to jquery/src/jquery because require.resolve('jquery') links to dist.

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