12

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 "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"

federico scamuzzi
  • 3,708
  • 1
  • 17
  • 24
Hivaga
  • 3,738
  • 4
  • 23
  • 36

5 Answers5

4

Try adding

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

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

Radovan Skendzic
  • 2,923
  • 1
  • 14
  • 22
1

Try this solution. Not sure it's official, but in my case it's working well. 3 problems should be resolved:

  1. TypeScript paths
  2. Compiled JS paths
  3. Working directory

First. TypeScript paths is resolved by adding "paths" into workspace/tsconfig.lib.json. NX does it automatically while lib gen. Look answer from Radovan Skendzic.

Second. Problem with compiled JS paths very good described here: Typescript paths not working in an Express project. So you need to install tsconfig-paths into your workspace:

yarn add -D tsconfig-paths

Third. Considering nx run [project]:[target] is working in workspace/ directory you should set CWD to libs/lib-b home directory - to find correct tsconfig.json

So, finally, you have the following executor (add this to your lib-b/project.json) that should work:

"targets": {
    "start-dev": {
        "executor": "@nrwl/workspace:run-commands",
        "options": {
            "commands": [
                "nodemon -e ts,js --exec ts-node -r tsconfig-paths/register src/index.ts"
            ],
            "cwd": "libs/lib-b"
        }
    },
    ...
}

Command to run:

nx run lib-b:start-dev
Le Ma
  • 67
  • 6
  • That works great! How do you use this solution in production? – falconizer Jul 28 '22 at 07:23
  • Not yet. I've just met this issue during dev - was trying func call btw two libs. StackOverflow is great - I instantly found the same problem from others and fill I'm not alone! – Le Ma Jul 28 '22 at 10:22
1

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

Haitham
  • 21
  • 4
1

Try adding lib-a as an implicit dependency of lib-b, add the line below into the libs/lib-b/project.json file and see what happens:

"implicitDependencies": ["lib-a"]

Running nx graph should show you a graph that should look something like this (do not consider the name of the libraries):

enter image description here

After that you should be able to build both libraries, I hope it works with you as well.

Rafael Berro
  • 2,518
  • 1
  • 17
  • 24
0

nx sets up paths in the tsconfig.base.json so that you can import from your libs within the monorepo without any futher setup.

If your monorepo is called my-monorepo and the lib you want to import from is called my-other-lib then you should be able to do: import { foo } from '@my-monorepo/my-other-lib';

See the docs for full info: https://nx.dev/concepts/more-concepts/applications-and-libraries

elltg
  • 21
  • 2