I have an example monorepo with 3 packages: back, front and shared. With the following folder structure:
root
├── back
│ ├── dist
│ ├── src
│ ├── test.ts
│ ├── package.json
│ └── tsconfig.json
├── front
├── shared
│ ├── dist
│ ├── src
│ ├── my-enum.ts
│ ├── package.json
│ └── tsconfig.json
├── package.json
└── tsconfig.json
test.ts
import { MyEnum } from '@test/shared/src/my-enum'
setTimeout(() => console.log(2 == MyEnum.B), 1000, 1)
my-enum.ts
export enum MyEnum{ A = 1, B, C }
When I build the back package typescript generates in the dist folder a test.js file with var my_enum_1 = require("@test/shared/src/my-enum");, however the compiled files of the shared package are in the dist folder, not in src, so if I try to run with node I get the following error:
Error: Qualified path resolution failed: we looked for the following paths, but none could be accessed.
Source path: E:\Repository\workspace_test\shared\src\my-enum
Not found: E:\Repository\workspace_test\shared\src\my-enum
Not found: E:\Repository\workspace_test\shared\src\my-enum.js
Not found: E:\Repository\workspace_test\shared\src\my-enum.json
Not found: E:\Repository\workspace_test\shared\src\my-enum.node
If I use ts-node instead of node it works, but that's not how I'd like to run this after deploy. I assume there's something I need to change in my tsconfig.json files but I can't find out what it is, other related SO questions I could find weren't very helpful.
I uploaded an example repo on github to make things easier to anyone willing to help, but here are my ts.config files:
Root:
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"sourceMap": true
}
}
Back:
{
"extends": "../tsconfig.json",
"compilerOptions": {
"composite": true,
"outDir": "dist",
"rootDir": "src"
},
"references": [
{ "path": "../shared" }
]
}
Shared:
{
"extends": "../tsconfig.json",
"compilerOptions": {
"composite": true,
"outDir": "dist",
"rootDir": "src"
}
}
Ok, so to fix this I had to change the package.json instead of the tsconfig.json. Making the main field in shared/package-json point to the dist folder doesn't do anything for some reason, but the exports field actually works, so creating an alias there solved the issue.
So for this example project it would be something like "exports": { "./src/my-enum": "./dist/src/my-enum.js" }.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With