external library not work in capacitor 4 with ionic


I’m trying to use an external library with Ionic 6 and Capacitor 4.
Works fine in browsers but crashes when trying from a device. Both iOS and Android.

I insert the external library in the index.html:


<script src="https://urlExample.js"></script>

I declare the variable used by the library, globally in my page.
I have tried declaring it with let, var or const, with the same result.


import { Component } from '@angular/core';

declare var ExampleVar: any;

  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
export class HomePage {

  constructor() {
    console.log(ExampleVar); //works fine


  startApi() {
       authorization: 'token'

The ExampleVar.start function works fine, it starts making the calls that the script has.

But it doesn’t seem to receive the token I send it.

This same code works fine in Capacitor 3, but when migrating to 4 it has stopped working and I don’t know why.


I answer myself.

The problem came from the change in capacitor version 4.3.0: https://github.com/ionic-team/capacitor/issues/5145

From this version onwards, to allow the sending of cookies and headers in third party calls it is necessary to manage it from the capacitor.config.ts, in the root of the project.

plugins: {
    CapacitorHttp: {
      enabled: false,
    CapacitorCookies: {
      enabled: true,

By adding this configuration, the script calls work correctly.

Answered By – Julia Benito

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published