ReferenceError: gapi is not defined, but I tried everything


This might sound like a dumb question because there are tons of questions of the exact same problem, but none of them could nearly help. I am using angular and in the ngAfterViewInit() I have following code: gapi.load("auth2", () => console.log("test"). I have installed @types/gapi and @types/gapi.auth2 via npm. In my I have included "gapi" and "gapi.auth2" in the types in compilerOptions. Also in the component’s html fiel I put <script src=""></script> in the head. I already tried to write declare var gapi: any; to the imports, but it didn’t work either. Also I tried the triple slas directive /// <reference path="../../../node_modules/@types/gapi/index.d.ts"> or /// <reference types="gapi">, but none of them worked. I have no clue what to do because I tried everything that I could find in hours of research. Thanks in advance 🙂


Try using a dynamic import:

import("").then(() => {
  gapi.load("client:auth2", () => {console.log("success")});

Answered By – Mario Varchmin

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