Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building library with imports from another library using NX Monorepo

Here is the case. I am using Nrwl NX Monorepo. I have 2 libraries: lib-a and lib-b; both are publishable libraries created via NX.

Now I create a MyClass.ts in lib-a. Naturally under paths in workspace/tsconfig.json > paths NX creates an alias to this lib-a ("@workspace/lib-a": ["libs/lib-a/src/index.ts"]).

So far so good. Now we can use this class anywhere within the workspace/monorepo by importing it via import { MyClass } from '@workspace/lib-a';

Unfortunately we can not build lib-b which is importing MyClass. When we try to do it we get the bellow error. So question is how can we build lib-b ?

PS: It seems strange that NX monorepo actually don't support such a common scenario linking 2 publishable libs.

"error TS6059: File "d:/workspace/libs/lib-a/src/index.ts" is not under 'rootDir' "d:\workspace\libs\lib-b\src" rootDir is expected to contain all source files"

like image 770
Hivaga Avatar asked Dec 08 '25 09:12

Hivaga


2 Answers

Try adding

"paths": { "@workspace/*": ["dist/libs/*"] }

into your tsconfig.lib.json files. This should resolve the problem.

like image 59
Radovan Skendzic Avatar answered Dec 12 '25 18:12

Radovan Skendzic


Don't override "baseUrl" and "paths" in any of child tsconfig! Put all of your "paths" in tsconfig.base.ts!

like image 26
Haitham Avatar answered Dec 12 '25 19:12

Haitham