meyousikmann meyousikmann - 1 year ago 309
Javascript Question

Importing bower component library into Angular 2 application

I am working on an Angular 2 application in ASP.NET Core and I am trying to import oidc-client ( into one of my Typescript files. The oidc-client files were installed via bower which placed them in the expected bower_components folder. The oidc-client.js file is copied to the lib folder under wwwroot via a gulp task on build.

My directory structure looks like this (only relevent portions represented):

| |
| --shared
| |
| --auth
| |
| --token.service.ts
| |
| --oidc-client
| |
| --oidc-client.js
| |
| --oidc-client
| |
| --dist
| | |
| | --oidc-client.js
| --oidc-client.d.ts

I need to use the oidc-client library in my token.service.ts file so I am doing this:

import { Oidc } from './lib/oidc-client/oidc-client';

But this results in an error 'Cannot find module './lib/oidc-client/oidc-client'.

I've tried different variations on the path, relative and non-relative, and I can't seem to figure out how to import the oid-client library.

Maybe its the typings not working? Do I need to do anything special with the d.ts file to make it work?

Thanks for any guidance.

Answer Source

As mentioned in a comment made above, using the NPM distribution of oidc-client would be the preferred option.

However, the typings that are included in the oidc-client are a little strange. Usually, the typings .d.ts file sits side-by-side with the .js file. With oidc-client, they are in different directories and that's what's causing problems with your import statement.

If you install the NPM module and add node_modules/oidc-client/oidc-client.d.ts to the files configuration of your tsconfig.json:

    "files": [

You should be able to create an index.ts file that imports oidc-client:

import { OidcClient } from "oidc-client";

If the .d.ts file and the .js file had been side-by-side, your import would have worked, but they are not, so it doesn't.

If you do not want to use the NPM distribution, it is likely that the separation of the .d.ts and .js files is what's causing the problem with the Bower distribution, too.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download