0

I have created a shared library and created new application feature library and I want to import shared library in the app feature library.

How can I import or include one library into another library?

1
  • One of the method is to pack this library by using github.com/ng-packagr/ng-packagr and then include it in your projects in the package.json Commented Oct 19, 2018 at 13:41

2 Answers 2

3

The best way is to use your shared library as a node dependency in your application library.

Example: If you have created your shared library via npm command as follows:

ng generate library sharedlib

Then build your shared library and publish in any public (npmjs) or private (your organization specific like JFrog) artifactory with following command

npm publish sharedlib 

And use it as dependency (add in package.json) in your application library as

"peerDependencies": {
    "@angular/common": "^7.2.0",
    "@angular/core": "^7.2.0",
    "sharedlib": "1.0.0"
}

I believe importing shared library module (from public-api.ts file) in application library (if both in same repository) can also work, but that is not a best way to do it.

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

Comments

-1

You should add any library you use as peerDependencies in your library's package.json. Something like this:

"peerDependencies": {
    "@angular/common": "^6.0.0-rc.0 || ^6.0.0",
    "@angular/core": "^6.0.0-rc.0 || ^6.0.0",
    "@angular/forms": "^6.0.0",
    "@angular/material": "^6.4.7"
}

Assuming you've created the library using the cli with ng generate library my-library, the peerDependencies node is already there.

This will avoid any conflict if the user of your library also uses the other ones too.

1 Comment

Your welcome! Please accept the answer if it was helpful.

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.