12

I have installed multiple libraries in angular 6 and I need to import one library in another. They depend on each other but it's not working

Here's how it goes:

ng generate library lib1

ng generate library lib2

Now in the main application, in package.json I add to tsconfig.json in compilerOptions the libraries (they are automatically inserted)

"paths": { "lib1": ["../distPack/lib1"], "lib2": ["../distPack/lib2"] }

(I modified the path where they are generated to be distPack)

I can import them in app.module and everything works fine

import { Lib1Module } from 'lib1'

PROBLEM

I want to: import { Lib1Module } from 'lib1' in lib2/src/lib/lib2.module.ts

And and it can't find it.

What I tried:

  1. peerDependency
  2. adding it to tsconfig.lib as paths
  3. including it as direct path (fails in --prod build)

Update

If I build the set the paths in main tsconfig.json to

"paths": { "lib1": [ "dist/lib1" ],

the other libraries can import it without a problem BUT it can no longer be imported in app.module

3
  • not sure if this helps, but I believe that you should list the lib1's dependency on lib2 as a peerDependency, and also have the library paths in tsconfig.json Commented Jul 9, 2018 at 2:57
  • I tried that, doesn't work Commented Jul 9, 2018 at 5:54
  • Can you share your code? Commented Jul 12, 2018 at 8:13

1 Answer 1

3

I want to: import { Lib1Module } from 'lib1' in lib2/src/lib/lib2.module.ts

And and it can't find it.

Based on this, when you import a library inside your module it'll search for it in same directory inside node_modules.

So if you want to include libr1 inside lib2 you should go to lib2 directory and install lib1 inside that one.

I hope this help you, If it isn't your answer tell me more about your problem

Sign up to request clarification or add additional context in comments.

9 Comments

True. It searches in node_modules. I'm referring here to ng generate library lib1. It's in Angular6 to make local libraries that you can later publish. If I do publish them on npm and reference them, yes, works. But it's supposed to work without publishing. I want everything local
remove .ts form lib2.module.ts
Tried doesn't work. Even trying using only public_api. Works in dev but not in prod
what happens in prod mode?
It can't find the libraries if they are imported like this import { Lib1Module } from 'lib1' even though they are correctly referenced and working in dev mode. Even when they are referenced with absolute paths they cannot depend on each other.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.