Next.js Discord

Discord Forum

Resolving .css from other package

Answered
Asian black bear posted this in #help-forum
Open in Discord
Avatar
Asian black bearOP
I have a monorepo setup where I have a next.js project that includes a packages. In that packages I want to load the css-string using the following notation:

import style from '../styles/main.css?inline'

this works perfect in dev mode, however in production, when running build, it says Type error: Cannot find module '../styles/main.css?inline' or its corresponding type declarations.

I assume that webpack/swc somehow does bundle the css if it comes directly from the next.js project code but not if it comes from a monorepo dependency. Any idea how I can get this to work?
Answered by Asian black bear
Okay as It turns out my problem was that this ?inline didnt work at all but for some reason turbopack (running dev) still accepts it somehow (thought it didnt use vite?) but build always didn't, so the solution was just to normally import in a react component and use that one
View full answer

21 Replies

Avatar
.
Can you share your structure of folder, and from folder you're running build from
Avatar
@Asian black bear
Avatar
Asian black bearOP
yes, the structure looks as follows:

/
/
node_modules/
apps/
├─ nextApp/
│  ├─ next.config.js
│  ├─ tsconfig.json
│  ├─ package.json
│  ├─ app/
packages/
├─ lib1/
│  ├─ src/
│  │  ├─ styles/
│  │  │  ├─ main.css
Avatar
You might need to run build from apps/nextApp
Avatar
Asian black bearOP
intrestingly, during build it still seems to resolve the package from lib1/src instead of lib1/dist
Avatar
Oh, during build, you're importing something outside nextApp folder
Avatar
Asian black bearOP
I am doing that, is there some further documentation to how transpilePackages in next.config.js works?
yes exactly during dev everything works
and the code also resolves during build (have tsconfig references)
Avatar
You can check out this Repo: https://github.com/rafaelalmeidatk/nextjs-forum/

It has packages/db, which is common for 2 projects(1 next)

it is having a custom import @ like @nextjs-forum, which directs to packages/db folder
Avatar
Asian black bearOP
thx will check it out!
Avatar
@Asian black bear If your issue is resolved, can you Mark solution?
Avatar
Asian black bearOP
yes will do if it solves it for me, trying if the same setup works for me (sadly they are not importing .css files which seems to be the problem in my case)
Avatar
Asian black bearOP
Mhh sadly it doesn't seem to work:

I now removed transpilePackages in the next.config and references from tsconfig as both are not used in the example.

With that it still produces the original error:

> next build

  ▲ Next.js 14.2.16
  - Environments: .env

   Creating an optimized production build ...
 ✓ Compiled successfully
   Linting and checking validity of types  ... ⚠ TypeScript project references are not fully supported. Attempting to build in incremental mode.
   Linting and checking validity of types  ..Failed to compile.

../../packages/lib1/src/Component1.tsx:17:22
Type error: Cannot find module '../styles/main.css?inline' or its corresponding type declarations.

  15 |
  16 | import type { TypeDef } from '..';
> 17 | import style from '../styles/main.css?inline';
     |                      ^
  19 |
npm error Lifecycle script `build` failed with error:
npm error code 1


No I tried using paths so I rewrote it to

import style from '@styles/main.css?inline';


and defined @styles in the tsconfig of lib1 as:

"compilerOptions": {
    ...
    "paths": {
      "@styles/*": [
        "./src/styles/*"
      ],
    }


and in the tsconfig of nextApp as: (because here swc will see the module)

"compilerOptions": {
    ...
    "paths": {
      "@styles/*": [
        "../../packages/lib1/src/styles/*"
      ],
    }


However, this produces the following error:

> next build

  ▲ Next.js 14.2.16
  - Environments: .env

   Creating an optimized production build ...
Failed to compile.

../../packages/lib1/src/Component1.tsx
Module not found: Can't resolve '@styles/main.css?inline'

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
../../packages/lib1/src/Page.tsx

> Build failed because of webpack errors
npm error Lifecycle script `build` failed with error:
npm error code 1
Avatar
Can you provide a [Minimal Reproduction Repo](https://nextjs-faq.com/minimal-reproduction-repository)
Avatar
Asian black bearOP
note that in both cases it correctly resolves the Component1.tsx form the package so general code-linkage works
yes will set one up
Avatar
Asian black bearOP
Right now I am struggling to reproduce the problem because in my reproduction the following happens:

- npm run dev & npm run build both fail and say that the module is undefined (in my actual project npm run dev works and is using the css)
- if I instead import the style without a name in the packages (import './main.css'), the style is correctly injecting into the main next-js app

so right now I try to find out where I got this "?inline" from, somehow neither the next.js nor the turbo documentation mention it, although I vividly remember reading that it exists (there was also a ?raw). Any idea where this loader doc is located?
Avatar
Asian black bearOP
Okay now I remember, this is from vite (https://vite.dev/guide/assets.html), which I use in another sub-package...
however for some strange reason it works in next.js (in dev mode) in my project
Avatar
Asian black bearOP
Okay as It turns out my problem was that this ?inline didnt work at all but for some reason turbopack (running dev) still accepts it somehow (thought it didnt use vite?) but build always didn't, so the solution was just to normally import in a react component and use that one
Answer