Next.js Discord

Discord Forum

Path Aliases in Monorepo

Unanswered
Sloth bear posted this in #help-forum
Open in Discord
Sloth bearOP
Hey community! I have trouble with path aliases inside a monorepo. Consider the following monorepo structure:

I have a nextjs project at apps/next and a ui package at packages/ui. For both packages I want to configure path aliases with individual, independent tsconfigs that both include these two rules:
    "baseUrl": ".",
    "paths": {
      "~/*": ["./*"]
    }

Now take a look at a hypothetical file inside packages/ui that looks basically like this:
// packages/ui/Card.tsx
import { something } from "~/lib/something" // packages/ui/lib/something.ts

export function Card() { ... }

I now want to import Card into my next app like this:
// apps/next/app/page.tsx
import { Card } from "@mymono/ui" // <- this fails!
import { Container } from "~/components/Container" // apps/next/components/Container.tsx

export default function Page() { ... }

Importing the Card component fails with the following error:
../../packages/ui/Card.tsx

Module not found: Can't resolve '~/lib/something'

For my understanding, this is because the path alias inside packages/ui is overridden by the path alias defined in apps/next. How can I fix this?

2 Replies

Masai Lion
I hate that this question has no answer. I'm trying to make nextJs respect tsconfig path aliases right now