How to import a library from another library in nx project

939 Views Asked by At

I have created a nextjs app using nx.

In the same project, I create 2 libraries:

utils-logger utils-logger-transports when I try to include utils-logger-transports in utils-logger I get cannot find "@nx-app/utils-logger-transports" module

the import paths are properly setup in tsconfig.base.json

The basic question is how can I create multiple libraries and import a library within others.

Thanks a lot for your help!

1

There are 1 best solutions below

1
On

I have a process on our back end that auto generates interfaces, enums and services that are necessary to call our back end. All of these get generated into three directories: interfaces, enums and services. I have put these directories into buildable library that is structured like this: /libs/api/src/{interfaces,enums,services} In each directory are the appropriate TS files.

In my tsconfig.base.json file, I have added the following path entries:

  "@skycourt/api/enums/*": ["libs/api/src/enums/*"],
  "@skycourt/api/interfaces/*": ["libs/api/src/interfaces/*"],
  "@skycourt/api/services/*": ["libs/api/src/services/*"],

In the second library, called helpers, I import a couple of the enums and interfaces like this: import { ActivityType } from '@skycourt/api/enums/ActivityType';

VSCode happily recognizes these paths. Lint happily recognizes these paths and I am able to run the jest tests in the second library that uses these paths.

However, when I try to build the helpers library, I get these errors:

Compiling TypeScript files for project "common-helpers"... libs/common/helpers/src/lib/ActivityIcon.ts:1:30 - error TS2307: Cannot find module '@skycourt/api/enums/ActivityType' or its corresponding type declarations.

1 import { ActivityType } from '@skycourt/api/enums/ActivityType'; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I know that Nx understands the dependency betwen helpers and api because when I display the graph, there is an arrow from helpers to api.strong text