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
rootDir
option in thetsconfig.json
file. And sincelibB
is outside of therootDir
oflibA
,... you get an error.You can start with adjusting the
paths
option in thetsconfig.json
to map@myscope/libB
toprojects/libB/src/public-api
. Make surelibB
has apublic-api.ts
file in itssrc
directory that exportsComponentX
.And:
Then make sure
libB
is built beforelibA
using a script or, if using a compatible TypeScript version, set up project references in thetsconfig.json
.With:
If necessary, adjust the
rootDir
compiler option to encompass bothlibA
andlibB
.You would have:
In
tsconfig.json
:In
projects/libB/src/public-api.ts
:Check then if
libA
is able to importComponentX
fromlibB
without encountering the'rootDir'
error, provided the build order is correctly managed (i.e.,libB
built beforelibA
).Yes, that is correct.
When
libB
is listed in thepeerDependencies
oflibA
, it signals thatlibB
is a requirement forlibA
, butlibB
will not be bundled withlibA
.Instead, it is expected that whoever is using
libA
will 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.