Resolving .css from other package
Answered
Asian black bear posted this in #help-forum
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:
this works perfect in dev mode, however in production, when running build, it says
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?
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
21 Replies
Can you share your structure of folder, and from folder you're running build from
@Asian black bear
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
You might need to run build from
apps/nextApp
Asian black bearOP
intrestingly, during build it still seems to resolve the package from lib1/src instead of lib1/dist
Oh, during build, you're importing something outside nextApp folder
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)
You can check out this Repo: https://github.com/rafaelalmeidatk/nextjs-forum/
It has
it is having a custom import
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
folderAsian black bearOP
thx will check it out!
@Asian black bear If your issue is resolved, can you Mark solution?
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)
Asian black bearOP
Mhh sadly it doesn't seem to work:
I now removed
With that it still produces the original error:
No I tried using paths so I rewrote it to
and defined @styles in the tsconfig of lib1 as:
and in the tsconfig of nextApp as: (because here swc will see the module)
However, this produces the following error:
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
Can you provide a [Minimal Reproduction Repo](https://nextjs-faq.com/minimal-reproduction-repository)
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
Asian black bearOP
Right now I am struggling to reproduce the problem because in my reproduction the following happens:
-
- if I instead import the style without a name in the packages (
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?
-
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 appso 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?
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
however for some strange reason it works in next.js (in dev mode) in my project
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