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

Cannot find module "jquery" #679

Closed
jashsayani opened this issue Sep 19, 2016 · 18 comments
Closed

Cannot find module "jquery" #679

jashsayani opened this issue Sep 19, 2016 · 18 comments

Comments

@jashsayani
Copy link

I am porting over some old code that uses jQuery. For now, I need jQuery in my project (will slowly get rid of it). I added it to my dependencies in package.json and ran npm install. I see jquery under node_modules now.

In my component file, when I add import $ from 'jquery';, I see this error:

Uncaught Error: Cannot find module "jquery" webpackMissingModule

I referred to #614 but still no luck.

@gaearon
Copy link
Contributor

gaearon commented Sep 19, 2016

Can you provide a project reproducing this?

@jashsayani
Copy link
Author

It should be easy to reproduce. Use:
create-react-app test

Then add the package:
npm install jquery --save

Now import it:
import $ from "jquery";

Note: I'm using jQuery 1.9.1

@gaearon
Copy link
Contributor

gaearon commented Sep 19, 2016

Works with 3.1.0:

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import $ from 'jquery';
import './index.css';

console.log('wat', $);

ReactDOM.render(
  <App />,
  document.getElementById('root')
);

screen shot 2016-09-19 at 10 27 28

I’ll give 1.9.1 a try now.

@gaearon
Copy link
Contributor

gaearon commented Sep 19, 2016

With [email protected] the only solution seems to be:

import 'jquery/jquery';
// now you can use window.$

With [email protected] and later, import works properly:

import $ from 'jquery';
// now you can use $

@gaearon gaearon closed this as completed Sep 19, 2016
@jashsayani
Copy link
Author

import 'jquery/jquery'; worked fine. Thanks!

@kevgathuku
Copy link

kevgathuku commented Dec 10, 2016

Hi @gaearon,
There seems to be a regression since importing the latest version of jQuery and trying to use bootstrap with it fails now.
I am constantly getting this error no matter how I try to include jQuery:
screen shot 2016-12-10 at 1 59 31 pm

I have tried this:

import $ from 'jquery';

window.jQuery = window.$ = $;

Package Versions:

"bootstrap": "^3.3.7",
"jquery": "3.1.0",

I have set up a test repo where I have reproduced this.
Thanks in advance.

@gaearon
Copy link
Contributor

gaearon commented Dec 10, 2016

@kevgathuku

Do you need to use Bootstrap itself? I would recommend React Bootstrap instead. It is compatible with Bootstrap CSS but implements JS part as React components.

@kevgathuku
Copy link

Thanks for the reply @gaearon

In this case I think I really need to since I am porting some plain HTML to React. Using React bootstrap would take much much longer.
In the meantime, I have resorted to using jQuery as well as Bootstrap's JS from a CDN.

@gaearon
Copy link
Contributor

gaearon commented Dec 10, 2016

@kevgathuku

Anyway, I sent a fix here: kevgathuku/react-bootstrap-jquery#1.

But I would recommend using React Bootstrap over Bootstrap itself. Since React takes care of DOM manipulation, it is often much more convenient to use React components than something like Bootstrap, although you could mix them if you want to.

@gaearon
Copy link
Contributor

gaearon commented Dec 10, 2016

I hope this helps!

@kevgathuku
Copy link

Worked like a charm. Thanks 😄

I'll probably port it to React bootstrap later on, but I needed to get over the barrier of having something to work with first, before I can improve it.

Thanks a lot for taking your time to help @gaearon. Really appreciate it 👍

@gaearon
Copy link
Contributor

gaearon commented Dec 10, 2016

No worries, if you have any problems please file issues! We’re happy to help.

@cr101
Copy link
Contributor

cr101 commented Dec 11, 2016

Btw, React Bootstrap does not currently support Bootstrap 4

@ulisesrmzroche
Copy link

ulisesrmzroche commented Feb 14, 2017

The reason is that you need to provide the plugin ("$" and/or the "jQuery") to webpack config. You'll have to eject though. I'm not sure if the argument about using React Bootstrap is universal because people may want to use it for the javascript animations, or for very custom stuff, or even using Bootstrap 4.

  // config/webpack.config.dev.js
  new webpack.ProvidePlugin({
        $: 'jquery',
        jQuery: 'jquery',
    }),

@gaearon
Copy link
Contributor

gaearon commented Feb 14, 2017

The reason is that you need to provide the plugin ("$" and/or the "jQuery") to webpack config.

You don’t need to. That’s one possible way of doing it, but my fix here is completely equivalent: https://github.com/kevgathuku/react-bootstrap-jquery/pull/1/files.

@cr101
Copy link
Contributor

cr101 commented Feb 15, 2017

reactstrap provides React components for Bootstrap 4

@weswithley
Copy link

weswithley commented Jun 29, 2017

if anyone uses jquery 3.x ,
please use import 'jquery/src/jquery';

@Aliaksandr3by
Copy link

btter use
import jQuery, * as $ from 'jquery/dist/jquery.js';

bootstrap
import 'popper.js/dist/popper.js';
import 'bootstrap/scss/bootstrap.scss';
import 'bootstrap/dist/js/bootstrap.js';

@lock lock bot locked and limited conversation to collaborators Jan 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants