Skip to content

Commit 0a1b705

Browse files
committed
build!: drop the CommonJS build (#1366)
The package ships only its ES module build, with an exports map and no main or module fields, as tree-shakeable packages of the Trigen baseline do. The default condition lets Node.js 20.19+ and 22.12+ require() it. The build clears dist first, so an old index.cjs cannot be published. The migration guide to v6 and the FAQ answer to "Cannot find module" explain how to load the ESM-only package in Node.js, Jest and TypeScript. BREAKING CHANGE: react-chartjs-2 is ESM only. Bundlers and Node.js 20.19+ or 22.12+ load it as before; older Node.js needs ESM code, and Jest needs to transform the package or run with ES modules support. See https://react-chartjs-2.js.org/docs/migration-to-v6/
1 parent 8f33494 commit 0a1b705

7 files changed

Lines changed: 54 additions & 14 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)

‎package.json‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,17 @@
2424
],
2525
"sideEffects": false,
2626
"types": "./dist/index.d.ts",
27-
"exports": "./src/index.ts",
27+
"exports": {
28+
".": "./src/index.ts",
29+
"./package.json": "./package.json"
30+
},
2831
"publishConfig": {
29-
"main": "./dist/index.cjs",
30-
"module": "./dist/index.js",
3132
"exports": {
32-
"types": "./dist/index.d.ts",
33-
"import": "./dist/index.js",
34-
"require": "./dist/index.cjs"
33+
".": {
34+
"types": "./dist/index.d.ts",
35+
"default": "./dist/index.js"
36+
},
37+
"./package.json": "./package.json"
3538
},
3639
"directory": "package",
3740
"linkDirectory": false
@@ -45,7 +48,9 @@
4548
"prepublishOnly": "run test build clear:package clean-publish",
4649
"postpublish": "pnpm clear:package",
4750
"emitDeclarations": "tsc --emitDeclarationOnly -p tsconfig.build.json",
48-
"build": "run -p [ vite build ] emitDeclarations",
51+
"clear:dist": "del ./dist",
52+
"build:dist": "run -p [ vite build ] emitDeclarations",
53+
"build": "run clear:dist build:dist",
4954
"storybook": "storybook dev -p 6006 --no-open",
5055
"storybook:build": "storybook build",
5156
"sandboxes:build": "pnpm -r --filter \"./sandboxes/**\" build",

‎vite.config.js‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,15 @@ export default defineConfig({
77
build: {
88
target: 'esnext',
99
lib: {
10-
// CommonJS stays: it was dropped once and restored in v5.1
11-
formats: ['es', 'cjs'],
10+
formats: ['es'],
1211
entry: {
1312
index: './src/index.ts'
1413
}
1514
},
1615
rolldownOptions: {
1716
external: id => /^(react|chart\.js)(\/|$)/.test(id),
1817
output: {
19-
topLevelVar: false,
20-
// rolldown adds the directive only when the sources have it, and CommonJS of the package always ran strict
21-
strict: true
18+
topLevelVar: false
2219
}
2320
},
2421
sourcemap: true,

‎website/docs/docs.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
exports.docs = [
2+
{ title: 'Migration to v6', slug: '/docs/migration-to-v6' },
23
{ title: 'Migration to v5', slug: '/docs/migration-to-v5' },
34
{ title: 'Migration to v4', slug: '/docs/migration-to-v4' },
45
{ title: 'Working with datasets', slug: '/docs/working-with-datasets' },

‎website/docs/faq/esm-only.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
---
22
slug: /faq/esm-only
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."
34
---
45

56
# How to fix "Cannot find module 'react-chartjs-2'" error?
67

7-
Upgrade to Chart.js v4.1 and react-chartjs-2 v5.1, where the CommonJS support was restored.
8+
Since v6, react-chartjs-2 is an ESM-only package, and the error means that something tries to load it as CommonJS:
9+
10+
- Node.js before 20.19 and 22.12 cannot `require()` an ES module: upgrade Node.js, or make your code ESM.
11+
- Jest runs tests as CommonJS: let it transform the package with `transformIgnorePatterns`, run it with ES modules support, or use Vitest.
12+
13+
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.

‎website/docs/migration-to-v6.md‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
slug: /docs/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+
# Migration to v6
7+
8+
v6 ships only the ES module build: the CommonJS build is gone. The components, their props and the event helpers did not change.
9+
10+
## ESM only
11+
12+
The package has a single entry, `dist/index.js`, which is an ES module. How your project loads it depends on the tool:
13+
14+
- **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.
15+
- **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()`.
16+
- **Jest** runs tests as CommonJS: either let it transform the package,
17+
18+
```js title="jest.config.js"
19+
export default {
20+
transformIgnorePatterns: ['node_modules/(?!(react-chartjs-2)/)']
21+
}
22+
```
23+
24+
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.
25+
26+
If none of that is possible yet, keep using react-chartjs-2 v5, which ships the CommonJS build.
27+
28+
## TypeScript
29+
30+
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.

0 commit comments

Comments
 (0)