Search code examples
next.jscss-modulesnrwl-nx

trouble using next-transpile-modules in @nrwl/nextjs monorepo


My understanding is that I fall into Group 1 as those who are;

running a [nextjs] monorepo and therefore they want to be able to import their other packages from node_modules.

And running into an error similar to this:

../../node_modules/@waweb/base-ui.theme.brand-definition/dist/brand-definition.module.scss CSS Modules cannot be imported from within node_modules. Read more: https://nextjs.org/docs/messages/css-modules-npm Location: ../../node_modules/@waweb/base-ui.theme.brand-definition/dist/index.js

The official solution is next-transpile-modules, but as soon as I add any packages to the list of modules, I start getting errors in CSS modules in local source.

../../libs/ui/src/lib/contact.module.css
CSS Modules cannot be imported from within node_modules.
Read more: https://nextjs.org/docs/messages/css-modules-npm
Location: ../../libs/ui/src/lib/learn-more.tsx

Import trace for requested module:
../../libs/ui/src/lib/learn-more.tsx
../../libs/ui/src/lib/home.tsx
./pages/index.tsx

This is repeated for all components that were previously working.

I have prepared a branch in a public repo that has a full ci/cd and gitpod dev env configured that demonstrates the critical change.

Let's assume the sources to the components I am attempting to transpile are located in the correct node_modules dir, and I am using the following next config:

// eslint-disable-next-line @typescript-eslint/no-var-requires
const withNx = require('@nrwl/next/plugins/with-nx');
const withPlugins = require('next-compose-plugins');
const withTM = require('next-transpile-modules')(
  [
    '@waweb/base-ui.theme.colors',
    '@waweb/base-ui.theme.color-definition',
    '@waweb/base-ui.theme.size-definition',
    '@waweb/base-ui.theme.shadow-definition',
    '@waweb/base-ui.theme.brand-definition',
    '@waweb/base-ui.theme.theme-provider',
  ],
  { debug: true }
);
const withPWA = require('next-pwa');

/**
 * @type {import('@nrwl/next/plugins/with-nx').WithNxOptions}
 **/
const nextConfig = {
  nx: {
    // Set this to true if you would like to to use SVGR
    // See: https://github.com/gregberge/svgr
    svgr: true,
  },
  images: {
    domains: [
      'www.datocms-assets.com',
      'a.storyblok.com',
      'images.ctfassets.net',
      'images.prismic.io',
      'cdn.aglty.io',
      'localhost', // For Strapi
    ],
    imageSizes: [24, 64, 300],
  },
};

const pwaConfig = {};

const plugins = [[withNx], [withPWA, pwaConfig]];

module.exports = withTM(withPlugins([...plugins], nextConfig));

Any idea what's wrong with my setup here?

Thank you all for any thoughts as to what I'm doing wrong here.

Cheers!

edit

For some additional context, I have tried many different variations, and the one I ended up on (shown above) is what got the module transpilation to actually work, according to the debug statements. Only now do I have the reported errors in modules that are actually source components, not node_modules. The usage of the plugin at all seems to break unrelated functionality.


Solution

  • It looks odd to me that you are wrapping withPuglins inside of withTM...

    withTM is a plugin so I would imagine it should be more this format:

    module.exports = withPlugins([
      withTM
    ], nextConfig);
    

    This seems to be what's expected when looking at the docs:

    https://www.npmjs.com/package/next-transpile-modules

    https://www.npmjs.com/package/next-compose-plugins