Building library with imports from another library using NX Monorepo
Asked Answered
N

8

13

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"

Neldanelia answered 15/7, 2019 at 19:23 Comment(6)
it's 2022 and still the same issue lolSavadove
it's 2023 and still I'm getting the same error :(Wirra
@ViranMalaka appreciate if you could share if you have a solution on thisMyrna
@Praveen, Thanks for bringing this up. I forgot to update my solution here. did you update your tsconfig.base.json file to change the alise to the lib? In my case that was the issue. if so that same alise name should be under the libs/<lib-name>/package.json -> name property. let me know the comment is not clear, I'll post a new answer.Wirra
None of the answers below have worked for me. Any updates on this?Pigtail
it's 2024 and still I'm getting the same error :( :(Archibald
H
8

Try adding

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

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

Hargeisa answered 2/9, 2020 at 12:22 Comment(2)
This is the only good answerIncreasing
Did not work for me :(Pigtail
H
3

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

Harewood answered 5/8, 2022 at 13:57 Comment(0)
E
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
Examine answered 27/7, 2022 at 4:13 Comment(2)
That works great! How do you use this solution in production?Sobel
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!Examine
M
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.

Millpond answered 14/8, 2022 at 1:55 Comment(1)
unfortunately this did not work for me. the graph was changed but the import still did not workPyromagnetic
H
1

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

Humanitarian answered 5/8, 2023 at 18:21 Comment(1)
That is without a doubt is the right way to do it. Thanks!Expostulate
F
1

adding rootDir to project.json options worked for me

"targets": {
"build": {
  "executor": "@nx/js:tsc",
  "outputs": ["{options.outputPath}"],
  "options": {
    "rootDir": ".",
Foghorn answered 6/11, 2023 at 22:51 Comment(0)
I
0

You need to specify the scope of each project through the 'tags' field in the project.json file. This ensures that the typescript does not complain during import, and your problem is resolved. Please pay attention to the following code.

for lib-a project edit the project.json file like this:

{
  // ... more project configuration here
  "tags": ["scope:lib-a"]
}

You can find much more information in the Nx documentation

Interlineate answered 2/10, 2023 at 14:15 Comment(0)
P
0

None of these worked for me, instead I had to change the angular version in lib-b's package.json to version 15:

  "peerDependencies": {
    "@angular/common": "^15.0.0",
    "@angular/core": "^15.0.0"
  },

I generated lib-b with an nx schematic and it generated this with version 16, but my lib-a was at version 15.0.0. I suspect the problem was not the particular version but that the versions need to match. Updated it manually and the project builds.

Penstock answered 22/3, 2024 at 14:12 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.