TSConfig Path Alias doesn't resolve in monorepo

Trying to convert my repo to a turborepo, using create-t3-turbo as a baseline. Almost everything worked as expected, until I tried to add custom path aliases inside packages/api. That led to the following error.
../../packages/api/src/trpc/router/meeting/meeting-attendance-link.ts:2:0
Module not found: Can't resolve '@/common/grant-rewards'
1 | import { applyLinkSchema } from "@meetxl/shared/schemas/link-schemas";
> 2 | import { grantRewards } from "@/common/grant-rewards";
3 | import { AttendanceLinkAction } from "@prisma/client";
4 | import { TRPCError } from "@trpc/server";
5 | import { randomBytes } from "crypto";

Import trace for requested module:
../../packages/api/src/trpc/router/meeting/meeting-attendance.ts
../../packages/api/src/trpc/router/meeting/index.ts
../../packages/api/src/trpc/router/index.ts
../../packages/api/index.ts
./src/pages/api/trpc/[trpc].ts

https://nextjs.org/docs/messages/module-not-found
../../packages/api/src/trpc/router/meeting/meeting-attendance-link.ts:2:0
Module not found: Can't resolve '@/common/grant-rewards'
1 | import { applyLinkSchema } from "@meetxl/shared/schemas/link-schemas";
> 2 | import { grantRewards } from "@/common/grant-rewards";
3 | import { AttendanceLinkAction } from "@prisma/client";
4 | import { TRPCError } from "@trpc/server";
5 | import { randomBytes } from "crypto";

Import trace for requested module:
../../packages/api/src/trpc/router/meeting/meeting-attendance.ts
../../packages/api/src/trpc/router/meeting/index.ts
../../packages/api/src/trpc/router/index.ts
../../packages/api/index.ts
./src/pages/api/trpc/[trpc].ts

https://nextjs.org/docs/messages/module-not-found
This is my tsconfig.json for the packages/api package:
{
"extends": "../../tsconfig.json",
"include": ["src", "index.ts", "transformer.ts"],
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
}
}
{
"extends": "../../tsconfig.json",
"include": ["src", "index.ts", "transformer.ts"],
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
}
}
The compilerOptions match apps/web exactly, but while it works in apps/web it very much does not work in packages/api.
6 Replies
iDarkLightning
iDarkLightningOP2y ago
It seems Next.js is unable to reconcile the different mappings in different packages?
Shoodey
Shoodey2y ago
I experienced a similar issue, I've only been able to use path aliases in nextjs app, not in a nodejs one nor a package
iDarkLightning
iDarkLightningOP2y ago
Yeah that's where I'm at as well
Shoodey
Shoodey2y ago
I actually did this on the a nodejs app -
// package.json
"imports": {
"#root/*": "./build/*.js",
"#utils/*": "./build/utils/*.js"
},
// package.json
"imports": {
"#root/*": "./build/*.js",
"#utils/*": "./build/utils/*.js"
},
// tsconfig.json
"baseUrl": ".",
"paths": {
"#root/*": ["./src/*"],
"#utils/*": ["./src/utils/*"]
},
// tsconfig.json
"baseUrl": ".",
"paths": {
"#root/*": ["./src/*"],
"#utils/*": ["./src/utils/*"]
},
it's not pretty, but does the job - for some reason i couldnt use ~ symbol there, but hashsign works didnt try with @ tbh, i dont like it lol
iDarkLightning
iDarkLightningOP2y ago
interesting, let me give that a shot hm doesn't seem to be working atm, I think I'm gonna take a break for now tbh been a long day lol
Rhys
Rhys2y ago
Imo it’s not worth using path aliases in a monorepo - they’re going to break more than they’re worth You can use my repo as a reference for how to implement them https://github.com/AnswerOverflow/AnswerOverflow I made one base.json tsconfig that has every path alias and then extend from that https://github.com/AnswerOverflow/AnswerOverflow/blob/main/packages/config/tsconfig/base.json It’s ugly and hacky but it works
GitHub
GitHub - AnswerOverflow/AnswerOverflow: Indexing Discord Help Chann...
Indexing Discord Help Channel Questions into Google - GitHub - AnswerOverflow/AnswerOverflow: Indexing Discord Help Channel Questions into Google
GitHub
AnswerOverflow/base.json at main · AnswerOverflow/AnswerOverflow
Indexing Discord Help Channel Questions into Google - AnswerOverflow/base.json at main · AnswerOverflow/AnswerOverflow
Want results from more Discord servers?
Add your server