Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Working notes for agents (and humans) contributing to this repository.

- **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.
- **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.
- **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.
- **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.

## Repository layout

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import { Doughnut } from 'react-chartjs-2';

## Docs

- [Migration to v6](https://react-chartjs-2.js.org/docs/migration-to-v6/)
- [Migration to v4](https://react-chartjs-2.js.org/docs/migration-to-v4/)
- [Working with datasets](https://react-chartjs-2.js.org/docs/working-with-datasets/)
- [Working with events](https://react-chartjs-2.js.org/docs/working-with-events/)
Expand Down
19 changes: 12 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@
],
"sideEffects": false,
"types": "./dist/index.d.ts",
"exports": "./src/index.ts",
"exports": {
".": "./src/index.ts",
"./package.json": "./package.json"
},
"publishConfig": {
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"exports": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"require": "./dist/index.cjs"
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./package.json": "./package.json"
},
"directory": "package",
"linkDirectory": false
Expand All @@ -45,7 +48,9 @@
"prepublishOnly": "run test build clear:package clean-publish",
"postpublish": "pnpm clear:package",
"emitDeclarations": "tsc --emitDeclarationOnly -p tsconfig.build.json",
"build": "run -p [ vite build ] emitDeclarations",
"clear:dist": "del ./dist",
"build:dist": "run -p [ vite build ] emitDeclarations",
"build": "run clear:dist build:dist",
"storybook": "storybook dev -p 6006 --no-open",
"storybook:build": "storybook build",
"sandboxes:build": "pnpm -r --filter \"./sandboxes/**\" build",
Expand Down
7 changes: 2 additions & 5 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@ export default defineConfig({
build: {
target: 'esnext',
lib: {
// CommonJS stays: it was dropped once and restored in v5.1
formats: ['es', 'cjs'],
formats: ['es'],
entry: {
index: './src/index.ts'
}
},
rolldownOptions: {
external: id => /^(react|chart\.js)(\/|$)/.test(id),
output: {
topLevelVar: false,
// rolldown adds the directive only when the sources have it, and CommonJS of the package always ran strict
strict: true
topLevelVar: false
}
},
sourcemap: true,
Expand Down
1 change: 1 addition & 0 deletions website/astro.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export default defineConfig({
label: 'Quickstart',
link: '/'
},
'docs/migration-to-v6',
'docs/migration-to-v5',
'docs/migration-to-v4',
'docs/working-with-datasets',
Expand Down
28 changes: 28 additions & 0 deletions website/src/content/docs/docs/migration-to-v6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
title: "Migration to v6"
description: "react-chartjs-2 migration guide to v6: the package is ESM only, the components and their props are the same."
---

v6 ships only the ES module build: the CommonJS build is gone. The components, their props and the event helpers did not change.

## ESM only

The package has a single entry, `dist/index.js`, which is an ES module. How your project loads it depends on the tool:

- **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.
- **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()`.
- **Jest** runs tests as CommonJS: either let it transform the package,

```js title="jest.config.js"
export default {
transformIgnorePatterns: ['node_modules/(?!(react-chartjs-2)/)']
}
```

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.

If none of that is possible yet, keep using react-chartjs-2 v5, which ships the CommonJS build.

## TypeScript

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.
9 changes: 7 additions & 2 deletions website/src/content/docs/faq/esm-only.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
---
title: "How to fix \"Cannot find module 'react-chartjs-2'\" error?"
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."
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."
---

Upgrade to Chart.js v4.1 and react-chartjs-2 v5.1, where the CommonJS support was restored.
Since v6, react-chartjs-2 is an ESM-only package, and the error means that something tries to load it as CommonJS:

- Node.js before 20.19 and 22.12 cannot `require()` an ES module: upgrade Node.js, or make your code ESM.
- Jest runs tests as CommonJS: let it transform the package with `transformIgnorePatterns`, run it with ES modules support, or use Vitest.

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.
Loading