2

I'm relatively new to React/Webpack and I want to apply Materialize CSS, but I am getting error about not recognizing jQuery in my code. I do not know where to begin.

I have a simple React Component as so:

src/component/header

import React, { Component } from 'react';
import { Navbar, NavItem } from 'react-materialize';
export default class Header extends Component {
  render() {
    return (
      <Navbar brand='logo' right>
        <NavItem href='get-started.html'>Getting started</NavItem>
        <NavItem href='components.html'>Components</NavItem>
      </Navbar>
    );
  }
}

package.json

{
  "name": "redux-simple-starter",
  "version": "1.0.0",
  "description": "Simple starter package for Redux with React and Babel support",
  "main": "index.js",
  "repository": "[email protected]:StephenGrider/ReduxSimpleStarter.git",
  "scripts": {
    "start": "node ./node_modules/webpack-dev-server/bin/webpack-dev-server.js",
    "test": "mocha --compilers js:babel-core/register --require ./test/test_helper.js --recursive ./test",
    "test:watch": "npm run test -- --watch"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.2.1",
    "babel-loader": "^6.2.0",
    "babel-preset-es2015": "^6.1.18",
    "babel-preset-react": "^6.1.18",
    "chai": "^3.5.0",
    "chai-jquery": "^2.0.0",
    "jquery": "^2.2.1",
    "jsdom": "^8.1.0",
    "mocha": "^2.4.5",
    "node-sass": "^3.8.0",
    "react-addons-test-utils": "^15.3.1",
    "webpack": "^1.13.2",
    "webpack-dev-server": "^1.14.0"
  },
  "dependencies": {
    "babel-preset-stage-1": "^6.1.18",
    "css-loader": "^0.24.0",
    "jquery": "^2.2.4",
    "lodash": "^3.10.1",
    "materialize-css": "^0.97.7",
    "node-sass": "^3.8.0",
    "react": "^15.3.1",
    "react-bootstrap": "^0.30.3",
    "react-dom": "^15.3.1 ",
    "react-materialize": "^0.15.2",
    "react-redux": "^4.0.0",
    "react-router": "^2.0.1",
    "redux": "^3.0.4",
    "sass-loader": "^4.0.0",
    "shortid": "^2.2.6",
    "style-loader": "^0.13.1"
  }
}

webpack.config.js

var path = require('path');
module.exports = {
  entry: [
    './src/index.js'
  ],
  output: {
    path: __dirname,
    publicPath: '/',
    filename: 'bundle.js'
  },
  module: {
    loaders: [{
      exclude: /node_modules/,
      loader: 'babel',
      query: {
        presets: ['react', 'es2015', 'stage-1']
      }
    },{
      test: /\.s?css$/,
      loaders: ['style','css','sass'],
      include: path.join(__dirname, 'src')
    }]
  },
  resolve: {
    extensions: ['', '.js', '.jsx'],
    alias: {
    'jquery': path.join(__dirname, 'node_modules/jquery/dist/jquery'),
    }
  },
  devServer: {
    historyApiFallback: true,
    contentBase: './'
  }
};

Error message:

Uncaught ReferenceError: $ is not defined

index.html

<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="http://fonts.googleapis.com/icon?family=Material+Icons">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.6/css/materialize.min.css">
  </head>
  <body>
    <div id="fcc">
      <div class="container"></div>
    </div>
  </body>
  <script src="/bundle.js"></script>
  <script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.6/js/materialize.min.js"></script>
</html>
2
  • 1
    Well, you're loading jQuery after the body, which you shouldn't, it should go inside the head or body, and jQuery should go before any other scripts that uses it, like materialize. Commented Aug 31, 2016 at 20:55
  • Yep, can't believe I totally missed that. Thanks! Commented Aug 31, 2016 at 21:06

1 Answer 1

2

Try including <script src="https://code.jquery.com/jquery-2.1.1.min.js"></script> before <script src="/bundle.js"></script>.

JQuery is used inside bundle.js but it's not defined yet.

Sign up to request clarification or add additional context in comments.

1 Comment

Oops! I totally missed that obvious thing. That did the trick :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.