Karan Deep Singh
Karan Deep Singh

Reputation: 81

How to import an external script in my Angular library?

I am trying to export some business logic ts files of my Angular application into an Angular library. Unfortunately, I need to use a Javascript file to connect to a proprietary service build by other people, which is only available as a pure javascript file and requires jQuery. Without libraries (when I was using the script in my angular application), I solved this by adding these js files to the .angular.json under the "scripts" section.

The js file is huge so I could not consider the option to rewrite in typescript.

Is there some way to import and use a javascript file in my ng library?

Thanks Karan

Upvotes: 4

Views: 3575

Answers (1)

Karan Deep Singh
Karan Deep Singh

Reputation: 81

I have finally solved my issue. Turns out there is no way to add a javascript file to an Angular library since ng library does not support "script" tag.

There is one other way: i.e. you can publish your library without the javascript file dependency, and the client using it will have to do 2 things:

  1. npm install "your-library"
  2. Now add the dependent javascript file to scripts tag in angular.json file.

Example in my case:

Run npm install wps-ng

And my Angular.json looks like this:

"scripts": [ "node_modules/jquery/dist/jquery.js", "node_modules/wps-js-52-north/src/web/lib/wps-js-all.js" ]

Unlike the other libraries, just a simple npm install is not sufficient, the client will just need to add their javascript file to scripts tag.

Upvotes: 2

Related Questions