recharts icon indicating copy to clipboard operation
recharts copied to clipboard

Error in nextjs with recharts

Open harshmangalam opened this issue 2 years ago • 28 comments

Server Error Error: require() of ES Module /home/l10006blr/Development/Nextjs/plants-shop/node_modules/d3-shape/src/index.js from /home/l10006blr/Development/Nextjs/plants-shop/node_modules/recharts/lib/shape/Symbols.js not supported. Instead change the require of index.js in /home/l10006blr/Development/Nextjs/plants-shop/node_modules/recharts/lib/shape/Symbols.js to a dynamic import() which is available in all CommonJS modules.

harshmangalam avatar Jul 28 '22 04:07 harshmangalam

Server Error Error: require() of ES Module /home/l10006blr/Development/Nextjs/plants-shop/node_modules/d3-shape/src/index.js from /home/l10006blr/Development/Nextjs/plants-shop/node_modules/recharts/lib/shape/Symbols.js not supported. Instead change the require of index.js in /home/l10006blr/Development/Nextjs/plants-shop/node_modules/recharts/lib/shape/Symbols.js to a dynamic import() which is available in all CommonJS modules.

i'm having the same issue as well

node: 16.1.0 next: 12.2.3 react: 18.2.0 recharts: 2.1.13

iamveekthorr avatar Jul 28 '22 06:07 iamveekthorr

Sounds like a duplicate of #2915

WolfyUK avatar Jul 28 '22 07:07 WolfyUK

I'm receiving the same error. Had to downgrade to 2.1.9 🤝

filipedeschamps avatar Jul 29 '22 00:07 filipedeschamps

The version I've been using is 2.1.9 and I haven't updated it. I had to change it to "recharts ": "^2.1.9""recharts": "2.1.9" 删除^后得到了帮助

ChaBen avatar Jul 29 '22 04:07 ChaBen

I had this error with Remix. I had to downgrade to 2.1.12.

charklewis avatar Jul 29 '22 04:07 charklewis

@charklewis great! 2.1.12 works here too 👍

filipedeschamps avatar Jul 29 '22 13:07 filipedeschamps

Same Error Downgrading didn't fix image

ochoadan avatar Jul 29 '22 16:07 ochoadan

Confirm the same issue when running yarn run build tested with

"recharts ": "^2.1.13 -> issue exists "recharts ": "^2.1.12 -> issue exists "recharts ": "2.1.12 -> works

image

HT-Moh avatar Jul 30 '22 17:07 HT-Moh

Is a fix planned for this issue? I also dwongraded to 2.1.12

peterjuras avatar Aug 04 '22 04:08 peterjuras

same error here

ghost avatar Aug 05 '22 05:08 ghost

same. downgraded to 2.1.9 and fixed version.

jackblackCH avatar Aug 05 '22 11:08 jackblackCH

Same. Downgraded to 2.1.12 bc 2.1.9 had react-resize-detector issue w/React v18.

donpark avatar Aug 06 '22 06:08 donpark

Downgrade didn't work for me. Next 12.2.4 & React 18.2.0

RicardoPBarbosa avatar Aug 06 '22 13:08 RicardoPBarbosa

@RicardoPBarbosa make sure you are pinning the version you want. So instead of ^2.1.12 force install 2.1.12

filipedeschamps avatar Aug 06 '22 15:08 filipedeschamps

Downgrade didn't work for me. Next 12.2.4 & React 18.2.0

Yeah same problem, i think its a problem with next. It worked when i implemented mantine-ui though.

Anyway i moved to d3js for Next.js because with almost 400 issues i doubt this issue will get fixed anytime soon.

ochoadan avatar Aug 06 '22 15:08 ochoadan

i have the same problem, here is not working even in version 2.1.12

dracoalv avatar Aug 06 '22 23:08 dracoalv

Works for me with

"next": "^12.1.4",
"react": "17.0.2",
"recharts": "2.1.12"

nephix avatar Aug 08 '22 20:08 nephix

I had this same problem in the past. Go to your packege.json and add "recharts": "2.1.9",

DelcioLuis avatar Aug 11 '22 11:08 DelcioLuis

changed

"recharts": "^2.1.13"

to

"recharts": "2.1.12"

and works

PS make sure to remove the ^ mark because I forgot to do it

fhodun avatar Aug 15 '22 14:08 fhodun

Your error come from d3 which has been upgrade to 7.x d3 7.x has a major change about distribution which is now in esm. You can read more about this https://devclass.com/2021/06/15/d3-7-0-goes-all-in-on-ecmascript-modules/

So you have to make your nextjs server friendly with esm From what I see the support on nextjs side is still on the roadmap but a hack exists: https://github.com/vercel/next.js/issues/706 https://www.npmjs.com/package/next-transpile-modules

jmfrancois avatar Aug 29 '22 08:08 jmfrancois

@jmfrancois it comes not from the package "d3", but from d3-shape Unfortunately, they didn't make a breaking change in that package like they are supposed to...

I guess it would have been nice to lock the version in recharts, but hindsight is 20/20

Itrulia avatar Aug 29 '22 16:08 Itrulia

changed

"recharts": "^2.1.13"

to

"recharts": "2.1.12"

and works

PS make sure to remove the ^ mark because I forgot to do it

nextjs 12.2.5, recharts 2.1.13, can not fix it with your way ,but thanks a lot.

DEV-XiA avatar Sep 06 '22 04:09 DEV-XiA

@Itrulia this is d3 global which is a set of d3-* package. d3-shape is the first which fail so nextjs stop but all of d3-XX packages are now pure ESM so they will fail into nextjs the same way.

This issue should be moved to d3 repository

jmfrancois avatar Sep 06 '22 07:09 jmfrancois

@jmfrancois I know, but the package isn't d3, it's d3-shape.

Itrulia avatar Sep 06 '22 08:09 Itrulia

The point is that it should be fixed on d3 side regardless of actual package name.

It could be fixed in recharts or even apps but it's going to get messy for everyone.

donpark avatar Sep 06 '22 23:09 donpark

The point is that it should be fixed on d3 side regardless of actual package name.

My point is that the package should be fixed in the package.json because the d3 package is following semver but the d3-shapes is not.

It's also important to be precise in a comment like this because people will check their packages and be confused why theres no d3 package.

Itrulia avatar Sep 07 '22 06:09 Itrulia

For those who using recharts@^2.1.3 with Next.js v12, this seems to work.

const path = require('path');

function generateIncludes(modules) {
  return [
    new RegExp(`(${modules.join('|')})$`),
    new RegExp(`(${modules.join('|')})/(?!.*node_modules)`),
  ];
}

const includes = generateIncludes(['recharts']);

const nextConfig = {
  experimental: {
    esmExternals: 'loose', // https://nextjs.org/docs/messages/import-esm-externals
  },
  webpack: (config, options) => {
    // Big thanks to https://github.com/d3/d3-format/issues/114#issuecomment-826338584
    config.externals = config.externals.map((external) => {
      if (typeof external !== 'function') return external;
      return ({ context, request, dependencyType, getResolve }, callback) => {
        return includes.find((i) =>
          i.test(
            request.startsWith('.') ? path.resolve(context, request) : request
          )
        )
          ? callback() // i.e., not an external
          : external(
              { context, request, dependencyType, getResolve },
              callback
            );
      };
    });
    return config;
  },
};

module.exports = nextConfig;

puku0x avatar Sep 30 '22 02:09 puku0x

thanks @puku0x , the issue can be close

jmfrancois avatar Sep 30 '22 14:09 jmfrancois

I suffered with the same issue (using recharts@^2.1.4 with Next.js v12.3.1)

I solved it by using dynamic import.

Before: I would imported the component which uses recharts

import Chart from "features/dashboard/Chart";

Now: Using dynamic import

import dynamic from "next/dynamic";

const Chart = dynamic(() => import("features/dashboard/Chart"), { ssr: false });

(I did not add webpack configuration into the config file.)

This solved my problem; hope helps you.

talatkuyuk avatar Oct 05 '22 20:10 talatkuyuk

Hi all,

Please upgrade to the latest recharts where d3 versions have been reverted.

Please watch for the merging and release of #3120 to fix the security vulnerability in d3-color. This change will be non breaking.

Thank you, Going to close this as resolved outside of 2.1.13 and 2.1.14

ckifer avatar Dec 30 '22 07:12 ckifer