I am developing a React component library. This library is used by several internal NextJs projects.
For compatibility reasons with NextJs, this library will need to be converted to CommonJs at some point.
The problem is that some npm dependencies of my library don't handle CommonJS. This is the case, for example, with swiper.
So I have two choices:
I can also envision a mix of the two: I ignore managing all dependencies for CommonJs and ES modules, and transpose only those that are necessary.
what do you think?
We have the same situation - we have a Next.js application that relies on a core library with underlying ESM modules. However, I recommend not bundling your dependencies. Your downstream applications may have the same dependencies and now you end up loading them twice. And you may have encountered this issue with other 3rd party dependencies (with ESM dependencies). Just add them to the
transpilePackages
list innext.config.js
: https://nextjs.org/docs/app/api-reference/ next-config-js/transpilePackages. We also use next/jest which also seems to get the configuration and seems to work. The only caveat is that I found out that we have to transpile@babel/runtime
but only when jest is running, otherwise it will break the main application.