I've encountered an issue in my monorepo where the tsconfig doesn't resolve paths that are located above the current project directory.
Here's an overview of my project structure:
── apps
│ ├── sahl
│ │ ├── index.d.ts
│ │ ├── jest.config.ts
│ │ ├── next-env.d.ts
│ │ ├── next.config.js
│ │ ├── project.json
│ │ ├── public
│ │ ├── src
│ │ ├── tsconfig.json
│ │ └── tsconfig.spec.json
├── libs
│ └── shared
│ ├── data-access
│ └── zod-types
├── nx.json
├── openapi.json
├── package.json
├── pnpm-lock.yaml
├── prisma
│ └── schema.prisma
├── tools
│ ├── database
│ ├── tsconfig.tools.json
│ └── zod-to-openapi.ts
└── tsconfig.base.json
My goal is to grant the directories in the "apps" folder access to files in the "libs/shared" directory.
Initially, I attempted to add these paths in tsconfig.base.json
and extend them to the tsconfig.json
of apps/sahl:
// ./tsconfig.base.json
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"rootDir": ".",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"target": "es2015",
"module": "esnext",
"lib": ["dom", "dom.iterable", "esnext"],
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"paths": {
"@libs/*": ["./libs/**/*.ts"],
}
},
"exclude": ["node_modules", "tmp"],
"include": ["./libs/shared/data-access/*.ts", "./libs/shared/zod-types/modelSchema/*.ts"],
}
However, it seems that the extension functionality of tsconfig is more of an overwrite than the merge I expected after further investigation.
Subsequently, I tried to include those paths and includes in the child tsconfig.json
:
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
// Compiler options here
},
"include": [
// Included paths here
],
"exclude": [
// Excluded paths here
]
}
Despite these efforts, I am facing difficulties with tsc not resolving the paths correctly during build time. It's puzzling as vscode and the "dev" functionality (e.g., next dev) are still functioning properly and recognizing the paths.
If anyone has any insights or suggestions, I would greatly appreciate it!
EDIT
Following @jagmitg's advice, I have removed baseUri from the child tsconfig.json
to circumvent conflicts:
// apps/sahl/tsconfig.json
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
// Compiler options here
},
"include": [
// Included paths here
],
"exclude": [
// Excluded paths here
]
}
and instead designated it in tsconfig.base.json
:
{
"compileOnSave": false,
"compilerOptions": {
// Compiler options here
},
"exclude": [
// Excluded paths here
],
"include": [
// Included paths here
]
}
As a result, I encountered a new error message:
- info Creating an optimized production build...
Failed to compile.
./src/app/api/articles/[articleId]/route.ts
Module not found: Can't resolve '@libs/shared/data-access/articles.data'
Could it be due to a typo or incorrect relative path in the includes?