Typescript path aliases in a monorepo #29399
Replies: 2 comments
-
|
Its impressive that monorepos are so popular nowadays and this question hasn't been answered since 2021. Another reason why NextJs is pure hype and doesn't even try to support sophisticaed edge cases |
Beta Was this translation helpful? Give feedback.
-
|
In a monorepo setup using TypeScript with Next.js, handling path aliases across packages can be challenging because Next.js performs TypeScript type-checking before Babel transpilation, which is why your build fails despite Babel correctly resolving the aliases. Recommended approaches
The build errors occur because Next.js type-checking happens before Babel resolves aliases. Using TypeScript project references or separating type-checking from build are the cleanest approaches. Ignoring build errors is a temporary workaround but maintaining proper project references ensures modularity and stability in a monorepo environment. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi there!
I'm working on a NextJS app written in TypeScript within a monorepo. The individual packages all have their tsconfig setup like this:
{ "compilerOptions": { // ... other settings "baseUrl": "./src", "paths": { "@/*": ["*"] }, }This allows me to take advantage of relative paths within all of the packages, and then when compiling NextJS with babel I can correctly transpile the aliases by making use of
babel-plugin-root-importlike this:This works fine for babel, but unfortunately when I do
next build, it appears to hit these aliases and blow up on the initial type checking stage which I can see from your source code takes place before babel transpilation.Note, that one of my goals in this project is to keep the individual monorepo packages as modular as possible, which means I don't want to put dependencies or config in the root of the project, and ideally individual packages (other than the
apppackage, which is unavoidable) shouldn't know anything about other packages beyond the workspace dependency import.Since I can do type-checking elsewhere, I have managed to get past type checking by adding
To my
next.config.jsfile, as I can probably do a full type check in the build separate to NextJS by using TypeScript project references, which I don't think you support, but it would be nice to have a solution that doesn't involve that.Beta Was this translation helpful? Give feedback.
All reactions