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 https://github.com/facebookincubator/create-react-app/issues/614 but still no luck.
Can you provide a project reproducing this?
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
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')
);
I鈥檒l give 1.9.1 a try now.
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 $
import 'jquery/jquery';
worked fine. Thanks!
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:
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.
@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.
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.
@kevgathuku
Anyway, I sent a fix here: https://github.com/kevgathuku/react-bootstrap-jquery/pull/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.
I hope this helps!
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 馃憤
No worries, if you have any problems please file issues! We鈥檙e happy to help.
Btw, React Bootstrap does not currently support Bootstrap 4
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',
}),
The reason is that you need to provide the plugin ("$" and/or the "jQuery") to webpack config.
You don鈥檛 need to. That鈥檚 one possible way of doing it, but my fix here is completely equivalent: https://github.com/kevgathuku/react-bootstrap-jquery/pull/1/files.
reactstrap provides React components for Bootstrap 4
if anyone uses jquery 3.x ,
please use import 'jquery/src/jquery';
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';
Most helpful comment
if anyone uses jquery 3.x ,
please use
import 'jquery/src/jquery';