-1

I create my own (private) npm packages based on Angular with Typescript.

I'm creating a package with a file named pdf-viewer.service.ts with the following content:

import { BehaviorSubject, Subject } from 'rxjs';
// ....
import * as $ from 'jquery';

@Injectable({
  providedIn: 'root'
})
export class PDFViewerService {
  constructor() {
    // $(.....)
  }
}

When I place this package directly in my main project, everything is working fine.

But when I build the package and publish it to my private repo and access it from there it isn't working.

I get this error:

ERROR TypeError: (jquery__WEBPACK_IMPORTED_MODULE_0___namespace_cache || jquery__WEBPACK_IMPORTED_MODULE_0___namespace_cache) is not a function

Some background info
After building and publishing the package it creates a PDFViewer.service.d.ts with the following content:

import { BehaviorSubject, Subject } from 'rxjs';
// ....
// Note that the jquery import is missing here!!!!!!!!!!!!

@Injectable({
  providedIn: 'root'
})
export declare class PDFViewerService {
  constructor();
}

How can i make this work?

1 Answers1

0

After spending some time on it, I have managed to find a solution.

I have added the following code to the angular.json file of my main project:

{
  ".....": {
    "architect": {
       "build": {
         "options": {
           "styles": [
             "...."
           ],
           "scripts": [
             "./node_modules/jquery/dist/jquery.js"
           ]
         }
       }
    }
  }
}

And changed the service to:

/// <reference types="jquery" />

import {BehaviorSubject, Subject} from 'rxjs';
declare var $: any;

// ....