What steps can be taken to implement jQuery within an Angular 15 npm package?

In my development process, I often create my own npm packages using Angular and Typescript.

One of the packages I am currently working on is a PDF viewer service, which includes a file named pdf-viewer.service.ts with the following code:

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

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

When I directly include this package in my main project, everything works perfectly without any issues.

However, once I build and publish the package to my private repository and try to access it from there, it fails to work properly.

The error message I encounter is:

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

A bit of context:
After building and publishing the package, it generates a PDFViewer.service.d.ts file with the missing jquery import, which may be causing the issue:

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

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

I am seeking advice on how to resolve this issue and make the package work correctly when accessed from the private repository. Any insights or solutions would be greatly appreciated.

Answer №1

After putting in some effort, I was able to come up with a resolution.

I made an update to the angular.json file in my main project by adding the following code:

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

Additionally, I modified the service to this:

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

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

// ....

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using redux action in the onPaginationChange function instead of setPaginationState in the given example for the TanStack table - is there a way to

Provided this sample Is there a way to utilize by dispatching a redux action rather than using useState - setPaginationState? onPaginationChange: state => dispatch(browseItemModalActions.setPagination(state)) An error is appearing in the console: ...

What is the best way to implement CSS rules for a hidden submenu?

Can anyone help me figure out how to apply CSS rules for a hidden submenu? I've attempted to use some JavaScript, but it's not working as expected. Below is a sample of the code I'm working with. The idea is that when you click on the anchor ...

Working with CodeIgniter and extracting data using an AJAX request

I've been experimenting with this code for a while now and I haven't been able to get it to work. I am working with CodeIgniter framework and when I click the button $('.galName').click(initUpdate);, it calls the following function: fu ...

Extracting the input's data and placing it into a table cell

Hey there! I am facing an issue where the data entered in the input fields is not getting transferred to the respective <td>. The text displayed in the (tabios) field should change according to what is entered in the input. Here's the code snipp ...

Disabling the intellisense feature for locale suggestions in Monaco is recommended

Switch the keyboard language to a different one (in this case Japanese using alt + shift), and when typing in Monaco editor, an intellisense menu appears with options to remove and search. Monaco Editor Version: V0.33.0 https://i.stack.imgur.com/SIyeV.pn ...

Updating the value of a time input tag using jQuery

I am currently trying to dynamically set the value of an HTML input tag with type="time" using jQuery. Here is the code for the HTML Tag: <input class="form-control" style="font-size: 1em;" type="time" name="time_star ...

After updating npm, it seems like it's not working due to the outdated version of nodejs

Due to not having root user access on my server, I was unable to update nodejs to resolve the issue at hand. Is there an alternative method to restore the npm version without utilizing the npm command in the terminal? (as the current nodejs version is too ...

Tips for guaranteeing the shortest possible period of operation

I am in the process of constructing a dynamic Angular Material mat-tree using data that is generated dynamically (similar to the example provided here). Once a user expands a node, a progress bar appears while the list of child nodes is fetched from the ...

`I am experiencing issues with the HTTP Post functionality`

Whenever I click on the button displayed in the index.html code, an error occurs when the "$http.post" call is made. The web page then displays an alert saying 'Error!', preventing me from saving the new JSON file due to this issue. I need help ...

The functionality of the submit form is encountering issues upon integration of Ajax requests

Hello everybody! I am currently creating a dynamic form for editing specific classes, but I am facing an issue with the ajax call not working. Below is the HTML code for the form: <form id="userDataForm" name="userDataForm" th:acti ...

Unable to establish headers once they have been issued - routing error

I have come across various solutions for this problem, but I am struggling to resolve it on my own. Therefore, I am sharing my code here in hopes of receiving assistance. I am new to this and would appreciate any help in understanding and fixing the issue ...

Should I use `npm install bootstrap` or manually download the source code

I'm currently experiencing some issues while trying to use Bootstrap's build tools. Specifically, I've been facing difficulties when using npm install bootstrap to set it up. Interestingly, the problem seems to disappear when I opt to manual ...

How can you simultaneously send FormData and String Data using JQuery AJAX?

Is there a way to upload both file and input string data using FormData()? For example, I have several hidden input values that also need to be included in the server request. html, <form action="image.php" method="post" enctype="multipart/form-data"& ...

Delightful Bootstrap Tabs with Dynamic Content via Ajax

My website has a lot of tabs designed with Bootstrap. I wanted to make them responsive, so I tried using a plugin called Bootstrap Tabcollapse from https://github.com/flatlogic/bootstrap-tabcollapse (you can see a demo here: http://tabcollapse.okendoken.co ...

My code is currently experiencing issues with jQuery

Here is the markup that I am using; <tbody> <tr> <td class="expand">Showroom area - New Display Zone</td> <td>300</td> <td>350</td> <td class="short ...

Which is more efficient for rendering performance: using images, CSS gradients, or box shadows with borders?

I'm curious about improving website scroll and animation performance. Which option would be better for your mobile webapp or website: Using a repeating thin image or CSS3 gradient? or Utilizing a repeating image instead of box shadow with a borde ...

Can you tell me the appropriate type for handling file input events?

Using Vue, I have a simple file input with a change listener that triggers the function below <script setup lang="ts"> function handleSelectedFiles(event: Event) { const fileInputElement = event.target as HTMLInputElement; if (!fileInp ...

Using sqlite3 on macOS Sierra is a powerful tool for

Each time I attempt to install sqlite3 on my Mac, I encounter the following error message: sh: node-pre-gyp: command not found npm WARN <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e095919d8ec4a596928692">[email p ...

Angular 4 allows for dynamically applying the active class to a clicked button, enhancing interactivity

Issue: <button *ngFor="let button of buttons" [ngClass]="{'active': isClicked}" (click)="isClicked = !isClicked" Description: A total of 10 buttons are displayed on the screen. When I click on button number 1, each button receives the clas ...

Ways to prevent recurring variables in Twitter bootstrap dialogues

I need assistance with deleting multiple links using ajax: <a id="id-1">link1</a> <a id="id-2">link2</a> <a id="id-3">link2</a> <a id="id-4">link2</a> ... This is the simplified version of my code: $(docum ...