reactjstypescriptrechartsvictory-chartsreact-starter-kit

Recharts is not working for the React with typescript


I have an issue with handling chart libraries.

Step by step

  1. Create an empty project

$ npx create-react-app chart-demo --template typescript

  1. Add Recharts

$ yarn add @types/recharts

  1. Run the project

$ yarn start

It shows the following error.

Failed to compile
./src/App.tsx
Module not found: Can't resolve 'recharts' in '\chart-demo\src'

FYI, it's not only for recharts but for several libraries. Fortunately, I found victory chart is working

It's really fresh a new project, just added recharts only.
Please help me to figure out this issue.

Package.json

{
  "name": "chart-demo",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "@types/jest": "^24.0.0",
    "@types/node": "^12.0.0",
    "@types/react": "^16.9.0",
    "@types/react-dom": "^16.9.0",
    "@types/recharts": "^1.8.15",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-scripts": "3.4.3",
    "typescript": "~3.7.2"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}


Solution

  • You didn't install recharts, you only installed its type definitions.

    Try:

    yarn install recharts
    

    When you install a @types/<package-name> package, you aren't installing any runnable code. You are just installing the type definitions for a package that doesn't provide it's own types. You still need to install the package itself.