create-react-app: Cannot find module "jquery"

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.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 18 (10 by maintainers)

Most upvoted comments

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

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

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.

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.

@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.

With jquery@1.x the only solution seems to be:

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

With jquery@2.x and later, import works properly:

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

reactstrap provides React components for Bootstrap 4

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