4

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:

  1. postcss-import - works but its weird to import css files within the shared module components from shared modules using full package name. I hear relative path root can be specified using from but not really good doc.
  2. tried something like @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

3
  • Does this answer your question: Is it possible to include one CSS file in another?? Try importing it as @import "@shared/shared.css"; in your CSS file. Commented Jul 9, 2021 at 14:03
  • hi @juliomalves. that does not solve my issue. The postcss-import solves it but i m trying to see if there is a better way without using that lib. Commented Jul 9, 2021 at 20:09
  • is there any way to import JS file from node modules? Commented Aug 26, 2022 at 5:17

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.