Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
    ]
  }
}

like image 448
incredever Avatar asked Sep 30 '20 19:09

incredever


People also ask

Can we use Recharts in react native?

To get started with Recharts, use a wrapper for React Native. Plunge into the gallery to take a look at the variety of options the library has to offer.

What is Recharts in material UI?

Recharts is a Redefined chart library built with React and D3. The main purpose of this library is to help you to write charts in React applications without any pain. Main principles of Recharts are: Simply deploy with React components.


2 Answers

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.

like image 54
Alex Wayne Avatar answered Sep 29 '22 09:09

Alex Wayne


It's also possible that you've installed rechart instead of recharts. make sure you install the correct package (recharts).

like image 42
Mahdi Ghajary Avatar answered Sep 29 '22 10:09

Mahdi Ghajary