37

I'm setting up a new React with the help of: Create React App

However, I'm running into a linting issue. I'm receiving the following linting error 'PropTypes' is not defined. (no-undef).

Here is the code that is causing the issue:

import React, { Component } from 'react';
import PropTypes from 'prop-types';

class Routers extends Component {
  static propTypes = {
    history: PropTypes.object.isRequired
  };

...

I tried playing around with the react/prop-types rule, but to no avail.

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Lars
  • 437
  • 1
  • 5
  • 11

7 Answers7

61

Since react 15.5, PropTypes is included in a separate package, 'prop-types'. So this line will help

import PropTypes from 'prop-types'

You can read more here

xskxzr
  • 12,442
  • 12
  • 37
  • 77
  • thanks! fixed my issue. but why have made this changes it breaks me some compiling. when running jest – Ido Bleicher Jul 30 '19 at 08:44
  • 1
    Also we need to change React.PropTypes to PropTypes if any. – Vivek Goel May 19 '20 at 05:28
  • This should definitely be the accepted answer. Especially since this issue is the first one showing up on Google and the issue being so trivial to encounter. I am starting a new project, with React 17.0.2 from create-react-app. I just activated eslint in IntlliJ and my project stoped working because it wasn't compliant with this rule. It's been 3 years since PropsType was migrated so now, it's old news and newcomers like me will have this issue more and more. – Netsab612 Aug 28 '21 at 16:02
  • For those who, like me, did not understand why propTypes is usefull in the first place : https://reactjs.org/docs/typechecking-with-proptypes.html – Netsab612 Aug 28 '21 at 16:02
  • You have to run `npm i -S prop-types` in the terminal in order to work with PropTypes – Zain Sadaqat Oct 19 '21 at 19:51
9

According to this issue comment.

It appears to be because you have installed eslint 4.x when you should just use the eslint version that is shipped with create-react-app. You should remove any eslint you have manually installed and use the one that comes with the repo.

Dan Mason
  • 2,177
  • 1
  • 16
  • 38
  • This appeared to be the issue. I ended up running `npm run eject` to unpack the configuration of the `react-create-app` package. After this I edited the package.json to use the eslint version 3. – Lars Aug 15 '17 at 13:03
  • If you mean that you installed eslint locally in your project, this is not really supported. I know that people do this, but it is not recommend. You don't need to install it locally, it will work out of the box. Installing it locally can mess up the versions, which is what probably happened. – Vivek Goel May 19 '20 at 05:27
4

I had the same problem on a project with eslint installed globally. I solved this case by installing eslint manualy in the project: npm i eslint --save

bye jeff

3

Please Install prop-types

using this code:

npm install --save prop-types
Penny Liu
  • 15,447
  • 5
  • 79
  • 98
testing
  • 79
  • 3
0

If you're using <script> tags; you can add this tag:

<script src="https://cdnjs.cloudflare.com/ajax/libs/prop-types/15.7.2/prop-types.min.js" integrity="sha512-ssNhh7jlzc+K93ckIlSXFHHz6fSFv0l619WOv8xbFNRbFOujbasb42LVMOggDrQR1ScJncoWb+KAJx1uF3ipjw==" crossorigin="anonymous"></script>

You can get the minified/non-minified and other version here

Good Luck...

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Aakash
  • 21,375
  • 7
  • 100
  • 81
-6

Please install the prop-types npm package - react 1.15 and greater on separate package is created.

Here to install package

Farzam Babar
  • 100
  • 6
-6

You can place the PropTypes just after the class (outside the class):

Routers.propTypes = {
   history: PropTypes.object.isRequired
}