My setup: I have a mono repo with multiple NextJS app sharing component from a shared module. The apps and shared module are individual workspaces managed by npm workspace. I m using css modules and post css for my NextJS apps.
Problem:
I want to import a css file from a shared module to css files in the NextJS apps. For example, I want to do something like @import @shared/shared.css in my css file for one of my component in the NextJS app.
Solutions tried:
from but not really good doc.@import ~@shared/shared.css. My IDE actually recognizes the path and is happy but the application fails to resolve the import and errors out like this: error - ../node_modules/next/dist/compiled/css-loader/cjs.js??ruleSet[1].rules[2].oneOf[2].use[1]!../node_modules/next/dist/compiled/postcss-loader/cjs.js??ruleSet[1].rules[2].oneOf[2].use[2]!./components/common/ToastAlert/ToastAlert.module.css TypeError: Cannot destructure property 'file' of 'undefined' as it is undefined.Thank you in advance
You can use next-transpile-modules package. The way you use it
// next.config.js
const withTM = require("next-transpile-modules");
const nextConfig = {...}
module.exports = withTM(["@shared"])(nextConfig);
Worked for me with no issues. I used rush.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With