Skip to content

Commit 93df085

Browse files
committed
docs: add the migration guide to v6
The guide and the FAQ answer to "Cannot find module" explain how to load the ESM-only package in Node.js, Jest and TypeScript.
1 parent 9d94c21 commit 93df085

5 files changed

Lines changed: 38 additions & 3 deletions

File tree

‎AGENTS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Working notes for agents (and humans) contributing to this repository.
88

99
- **Stay thin.** Anything Chart.js already does belongs to Chart.js. New props and options are added only when they cannot be expressed through Chart.js `options`, `plugins` or a ref to the chart instance.
1010
- **Bundle size matters.** The package is tree-shakeable and its size is a tested contract (`.size-limit.json`): a consumer pays only for the components they import. Follow the `tree-shaking` and `size-limit` skills.
11-
- **Compatibility is part of the API.** The package supports every React version from the `peerDependencies` range and ships both ESM and CommonJS builds: CommonJS support was removed once and restored in v5.1 after users broke. Changing either is a breaking change.
11+
- **Compatibility is part of the API.** The package supports every React version from the `peerDependencies` range. Since v6 it ships only the ES module build, which Node.js 20.19+ and 22.12+ can also `require()`. Narrowing either is a breaking change.
1212

1313
## Repository layout
1414

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import { Doughnut } from 'react-chartjs-2';
6060

6161
## Docs
6262

63+
- [Migration to v6](https://react-chartjs-2.js.org/docs/migration-to-v6/)
6364
- [Migration to v4](https://react-chartjs-2.js.org/docs/migration-to-v4/)
6465
- [Working with datasets](https://react-chartjs-2.js.org/docs/working-with-datasets/)
6566
- [Working with events](https://react-chartjs-2.js.org/docs/working-with-events/)

‎website/astro.config.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export default defineConfig({
9090
label: 'Quickstart',
9191
link: '/'
9292
},
93+
'docs/migration-to-v6',
9394
'docs/migration-to-v5',
9495
'docs/migration-to-v4',
9596
'docs/working-with-datasets',
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
title: "Migration to v6"
3+
description: "react-chartjs-2 migration guide to v6: the package is ESM only, the components and their props are the same."
4+
---
5+
6+
v6 ships only the ES module build: the CommonJS build is gone. The components, their props and the event helpers did not change.
7+
8+
## ESM only
9+
10+
The package has a single entry, `dist/index.js`, which is an ES module. How your project loads it depends on the tool:
11+
12+
- **Bundlers** (Vite, webpack 5, Rollup, esbuild, Parcel) and frameworks built on them, such as Next.js, import ES modules as they are: nothing to change.
13+
- **Node.js** 20.19, 22.12 and later can `require()` an ES module, so server-side rendering from CommonJS keeps working there. Older Node.js versions need your code to be ESM, `"type": "module"` in `package.json`, or a dynamic `import()`.
14+
- **Jest** runs tests as CommonJS: either let it transform the package,
15+
16+
```js title="jest.config.js"
17+
export default {
18+
transformIgnorePatterns: ['node_modules/(?!(react-chartjs-2)/)']
19+
}
20+
```
21+
22+
or run Jest with [ES modules support](https://jestjs.io/docs/ecmascript-modules), or move the tests to [Vitest](https://vitest.dev), which runs ES modules natively.
23+
24+
If none of that is possible yet, keep using react-chartjs-2 v5, which ships the CommonJS build.
25+
26+
## TypeScript
27+
28+
The types are published for ES modules only. With `"moduleResolution": "node16"` or `"nodenext"` in a CommonJS project, TypeScript asks to import the package with `import()`: use `"moduleResolution": "bundler"` when a bundler builds the project, or make the project ESM.
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
---
22
title: "How to fix \"Cannot find module 'react-chartjs-2'\" error?"
3-
description: "Fix the \"Cannot find module 'react-chartjs-2'\" error: upgrade to Chart.js 4.1 and react-chartjs-2 5.1, which restored CommonJS support."
3+
description: "Fix the \"Cannot find module 'react-chartjs-2'\" error: react-chartjs-2 v6 is ESM only, so load it as an ES module or stay on v5."
44
---
55

6-
Upgrade to Chart.js v4.1 and react-chartjs-2 v5.1, where the CommonJS support was restored.
6+
Since v6, react-chartjs-2 is an ESM-only package, and the error means that something tries to load it as CommonJS:
7+
8+
- Node.js before 20.19 and 22.12 cannot `require()` an ES module: upgrade Node.js, or make your code ESM.
9+
- Jest runs tests as CommonJS: let it transform the package with `transformIgnorePatterns`, run it with ES modules support, or use Vitest.
10+
11+
See the [migration guide to v6](/docs/migration-to-v6) for the details. If you cannot load ES modules yet, use react-chartjs-2 v5, which ships the CommonJS build.

0 commit comments

Comments
 (0)