Suppose an Angular workspace with two libraries inside (e.g. projects/libA and projects/libB). In libA I'm importing code from libB
import { ComponentX } from '@myscope/libB';
ComponentX is exported in index.ts located in projects/libB/src. In tsconfig.json (in root of worspace I have)
{
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"moduleResolution": "node",
"importHelpers": true,
"target": "ES2022",
"module": "es2020",
"skipLibCheck": true,
"allowSyntheticDefaultImports": true,
"lib": [
"es2018",
"dom"
],
"paths": {
"@myscope/libB": [
"projects/libB/src"
]
},
"useDefineForClassFields": false
}
}
In projects/libA/package.json I have
{
"name": "@myscope/libA",
"version": "0.0.1",
"peerDependencies": {
"@angular/common": "^15.0.0",
"@angular/core": "^15.0.0",
"@myscope/libB": "*"
},
"dependencies": {
"tslib": "^2.3.0"
},
"exports": {
".": {
"sass": "./src/styles.scss"
}
}
}
Still while calling ng build libA I get many errors like
The file <path_to_worspace>/projects/libB/src/.../component-x.ts is outside of the configured 'rootDir'.
How can I fix that?
I would assume you have the following project structure:
In that case, the Angular build system is checking the files being imported against the
rootDiroption in thetsconfig.jsonfile. And sincelibBis outside of therootDiroflibA,... you get an error.You can start with adjusting the
pathsoption in thetsconfig.jsonto map@myscope/libBtoprojects/libB/src/public-api. Make surelibBhas apublic-api.tsfile in itssrcdirectory that exportsComponentX.And:
Then make sure
libBis built beforelibAusing a script or, if using a compatible TypeScript version, set up project references in thetsconfig.json.With:
If necessary, adjust the
rootDircompiler option to encompass bothlibAandlibB.You would have:
In
tsconfig.json:In
projects/libB/src/public-api.ts:Check then if
libAis able to importComponentXfromlibBwithout encountering the'rootDir'error, provided the build order is correctly managed (i.e.,libBbuilt beforelibA).Yes, that is correct.
When
libBis listed in thepeerDependenciesoflibA, it signals thatlibBis a requirement forlibA, butlibBwill not be bundled withlibA.Instead, it is expected that whoever is using
libAwill also install and providelibB. That is a common pattern for avoiding duplicate bundling of libraries, especially in scenarios where multiple libraries are expected to interact with a shared instance of another library.