My current challenge involves setting up a TypeScript based monorepo using Lerna. In this setup, I have two packages named bar and foo. The issue arises when foo tries to import bar using a path alias and encounters an error.
tree
.
├── lerna.json
├── package.json
└── More file structure ...
Among the key configuration files:
- ./tsconfig.build.json
{
"compilerOptions": {
"baseUrl": ".",
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"declaration": true
}
}
- ./tsconfig.json
{
"extends": "./tsconfig.build.json",
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@company/bar": [
"packages/bar"
],
"@company/foo": [
"packages/foo"
]
}
}
}
- ./lerna.json
{
"packages": [
"packages/*"
],
"version": "0.0.0"
}
- ./package.json
{
"name": "root",
"private": true,
"scripts": {
"tsc": "lerna run tsc"
},
"devDependencies": {
"lerna": "^3.22.1",
"ts-node": "^9.0.0",
"ts-node-dev": "^1.0.0-pre.63",
"typescript": "^4.0.3"
}
}
In the case of Package bar:
- File paths and configurations for bar package...
And for Package foo:
- File paths and configurations for foo package...
When running npm run tsc
, the console outputs an error when foo attempts to import bar:
Error message detailing the module import issue...
The error is well defined, but I am unsure how to resolve it considering that my path aliases in ./tsconfig.json (3) seem correct. Can anyone identify where I might be going wrong with my configurations? Any insights or suggestions on fixing this would be greatly appreciated.
Although switching the import statement from
import { bar } from '@company/bar';
to import { bar } from '../../bar/src';
resolves the issue, I prefer sticking to the initial way of importing bar
.