17

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;
}

Skel
  • 1,611
  • 3
  • 16
  • 36

6 Answers6

17

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/

Skel
  • 1,611
  • 3
  • 16
  • 36
  • 1
    Thanks, that fixed it for me. The autogenerated path was not a relative one. – FloppyNotFound May 05 '20 at 15:31
  • Overriding baseURL in local tsconfig.json meant that it wasn't working – but this fixed it. Thanks! – Jackolai Jun 08 '21 at 09:28
  • @FloppyNotFound in which tsconfig did you update the path? – Alex May 13 '22 at 12:32
  • 1
    @Alex 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. – Skel May 13 '22 at 12:36
  • 1
    @Skel 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? – Alex May 13 '22 at 12:38
  • @Alex 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 helps – Skel May 13 '22 at 14:04
10

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.

Ari Seyhun
  • 11,506
  • 16
  • 62
  • 109
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.

chabu
  • 111
  • 1
  • 7
  • Yeah, I didn't come across that fix of deleting your node_modules file. Otherwise, I would have just done that. – Skel Jun 21 '19 at 10:18
  • 3
    Also, to note, this happens only for the workspace libraries(typescript data libraries, non-angular libraries, etc.). – chabu Jun 25 '19 at 04:06
  • I tried your solution and it didn't work as of Nov/2021 – Will de la Vega Nov 19 '21 at 01:50
  • 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 tree – Paul Carroll Mar 19 '23 at 22:17
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.

Paul Ryan
  • 1,509
  • 12
  • 26
0

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

MrJH
  • 11
  • 3
-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

super7egazi
  • 706
  • 1
  • 8
  • 22