NRWL NX importing lib error TS2307: Cannot find module '@eduboard/interfaces'
Asked Answered
T

7

19

I've just created a new NX project for work and I've created a lib for out interfaces to have them on the backend and the front end.

I'm getting this error when I compile

apps/askeddi/src/app/pages/global-admin/global-admin.component.ts(5,38): error TS2307: Cannot find module '@eduboard/interfaces'.

From everything i have read is that i have done nothing wrong but its asking for a module and its just an index.ts file.

export * from './lib/user';
export * from './lib/global-admin-dashboard';

And this the global-admin-dashboard

interface Schools {
  total: number;
  active: number;
  usingAssessor: number;
}

interface TotalNActive {
  total: number;
  active: number;
}

export interface GlobalAdminDashboard {
  schools: Schools;
  schoolGroups: TotalNActive;
  users: TotalNActive;
}

Tijuanatike answered 14/6, 2019 at 18:8 Comment(4)
is there a path entry in your tsconfig.json file for your library?Gillan
Yeah. That's why I'm really confusedTijuanatike
well it could be that there is a empty interfaces file in the template, that was the case in my nx project. just take a look at the index.ts in your lib and remove everything that is not necessary, also empty files.Gillan
Checked and none are empty still getting the no moduleTijuanatike
T
18

I found out how to fix my problem.

So inside the tsconfig.app.json file, I added this to the paths.

"@eduboard/interfaces" : [
  "../../../libs/interfaces/src/index"
  ]

I had to go back a fair few because I had it have a baseURL set to src/

Tijuanatike answered 17/6, 2019 at 13:9 Comment(6)
Thanks, that fixed it for me. The autogenerated path was not a relative one.Lodgment
Overriding baseURL in local tsconfig.json meant that it wasn't working – but this fixed it. Thanks!Champion
@Lodgment in which tsconfig did you update the path?Macdonald
@Macdonald They should all be linked together but sometimes you have to do some trial and error. I've Had a problem where with the hierarchy over write what I've done.Tijuanatike
@Tijuanatike thanks for getting back so fast! so in the root folder/tsconfig.base.json, I have "paths": { "@lib-frontend/ui": ["libs/ui/src/index.ts"] }, this is where I need to update the path?Macdonald
@Macdonald In my latest project i have a tsconfig.json file that's just got references to the paths of the other files and inside my tsconfig.app.json it extends the tsconfig.base.json then inside that, I have my "baseUrl": "./" Then my paths set up to start from src/ hope this helpsTijuanatike
E
15

You need to specify the location of your library for TypeScript to locate.

Add your library to the tsconfig.json at the root of your project under "paths":

{
  "compilerOptions": {
    ...
    "paths": {
      "@package/my-lib": ["libs/my-lib/src/index.ts"]
    }
  }
}

You may need to reload VSCode to have TypeScript reload the tsconfig.json.

Elspet answered 2/6, 2020 at 9:7 Comment(3)
I don't know why your answer was voted down. It helped me. I added paths to a few different places and nothing worked until I created a new tsconfig.json file and let it extend the tsonconfig.base.jsonSystematism
I'm glad it could help you at least :)Elspet
Restarting my IDE after making these changes was needed for meMedrano
H
5

nx team should pay some attention to this issue. I had same issue when I was trying out the tutorial on the nx.dev website. I read on some other thread that if you delete the node_modules folder and re-run npm install, the issue disappears. I tried it and it worked. Really disappointing.

As mentioned in the other answer, I don't think you need to specify paths in every project where you use the library. That breaks some basic architectures/benefits provided by nx.

Honghonied answered 20/6, 2019 at 3:47 Comment(4)
Yeah, I didn't come across that fix of deleting your node_modules file. Otherwise, I would have just done that.Tijuanatike
Also, to note, this happens only for the workspace libraries(typescript data libraries, non-angular libraries, etc.).Honghonied
I tried your solution and it didn't work as of Nov/2021Horseweed
For anyone following this in the future... while I didn't need to delete node_modules, it could be that you just need to delete node_modules/.cache/nx instead of the whole treeLecithinase
G
1

DON'T play with NX configuration. Just delete dist and node_modules and install again. I think it is a bug that they need to fix

Gambia answered 21/2, 2023 at 22:0 Comment(0)
S
0

For those that upgraded from nx 6/7 to 8 or 9 you may need to verify your angular.json for libraries is is using "builder": "@nrwl/angular:package", instead of "builder": "@angular-devkit/build-ng-packagr:build",. Without this change the system will attempt to use the angular-cli builder which has no knowledge of the rest of the workspace.

Synaeresis answered 3/5, 2020 at 20:28 Comment(0)
O
0

I fixed it with the idea of chabu, but I only do an npm i without deleting the node_modules folder.

Oceanus answered 14/4, 2022 at 9:52 Comment(0)
E
0

I have faced a similar issue, I have forget to write ".ts" extension of a file and VSCode has auto interprete the file as a TS file.

So, in my case, just fix the extension file by adding ".ts" solve my issue.

Erma answered 9/10, 2023 at 5:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.