1

I'm creating a custom library in Angular 7 and I would like to somehow export a function in this library that can be used in the main application after the library is installed. I saw some documentation about doing this through a typings.d.ts file, but after trying it out, it seems the typings file can only be installed through npm, which doesn't work for me since this library will not be uploaded to npm. Is there a way to export a function without having to upload the library to npm?

2
  • You mean, you want to use it as local npm module? Commented Feb 24, 2020 at 3:15
  • I am also using an angular project(repo1) as a dependency in another angular project(repo2). I want to export some common functions from repo1 that can be used in repo2. This is compiling successfully but giving an error on the browser console. Uncaught TypeError: (0 , som_core__WEBPACK_IMPORTED_MODULE_0__.functionName) is not a function. Did you find any solution ? Commented Dec 5, 2021 at 5:54

3 Answers 3

1

I ended up doing a different approach. I set up my custom library to take IN a function to be executed after a save function was completed, instead of exporting the library's function. I set it up just like how the chosen answer was done here: Angular 5 Component input function that takes parameters

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

Comments

0

to use local library use relative path in package.json as below:

"@libraryName":"file:../../so_on/lib_workspaceName/dist/my-lib"

then run npm install in main application.

Comments

0

After some digging I was able to figure this out...

Add this line to your public-api.ts file in your custom library. (No file extension needed here, just like the other lines you'll have in there; And it should auto-complete as you type it out.)

export * from './lib/some-path/file-name-that-has-functions';

Then import your function in the application that's using the library.

import {myFunctionName} from "my-custom-library";

You would do the same thing if you had an enum, a variable or some other exported member you wanted to use in the client application but keep in a custom library.

It's been a few years, but if I was looking for it someone else probably is too. Just remember to ng build my-custom-library after making this change!

Lots more info about this here.

Comments

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.