As I work with nestjs, I found it convenient to create a "common" folder that can be shared between the front and back applications:
project
│
└───common
│ │
│ └───dtos
│ │ dto-a.ts
│ │ dto-b.ts
│
└───api (==> nestjs)
│ │
│ └───src
│
└───front
│
└───src
To make this work, I added the following to the tsconfig files in both the front and back applications:
{
"compilerOptions": {
"paths": {
"@common/*": ["../common/*"]
}
}
}
Now, I can easily import the models using the following syntax:
import { DtoA} from "@common/dtos";
However, after making this change, I noticed that the compilation process is creating two separate folders under the dist
folder:
dist
│
└───common
│
└───api
Unfortunately, nestjs is unable to find the necessary modules under dist/main
and encounters errors:
Error: Cannot find module 'path\to\project\api\dist\main' at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1039:15) at Function.Module._load (node:internal/modules/cjs/loader:885:27) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:23:47
To resolve this issue, I need to instruct nestjs to look into dist/api/main
or find a way to bundle the common folder within the api build. How can I achieve this?