0

Before I get marked for duplicate, I have already tried the solution to a very similar problem here and updated the typings in my comiplerOptions in tsconfig.json as this solution.

Basically I have created an Angular Service (Angular 8, which is why the previous solutions might not have worked) that calls the Google Api (gapi) for Google SignIn.

@Injectable({
  providedIn: 'root'
})
export class UserService {

  constructor(private router: Router) { }

  gapi: any; //solution from link one which allows me to run it locally

  googleInit() {
    gapi.load('auth2', () => {
      gapi.auth2.init({
        ...
      })
    });
  }
  ... (other gapi calls)

}

This service works completely fine on my local server (localhost:4200) even though there is an error message from the compiler.

 ERROR in src/app/shared/services/user/user.service.ts(25,12): error TS2663: Cannot find name 'gapi'. Did you mean the instance member 'this.gapi'?

However, when I try to run npm run build to deploy it on a server, the compiler crashes and displays "This is probably not a problem with npm. There is likely additional logging output above."

The log is displayed as

0 info it worked if it ends with ok
1 verbose cli [
1 verbose cli   '/usr/local/Cellar/node/12.4.0/bin/node',
1 verbose cli   '/usr/local/bin/npm',
1 verbose cli   'run',
1 verbose cli   'build'
1 verbose cli ]
2 info using npm@6.10.2
3 info using node@v12.4.0
4 verbose run-script [ 'prebuild', 'build', 'postbuild' ]
5 info lifecycle pos@0.0.0~prebuild: pos@0.0.0
6 info lifecycle pos@0.0.0~build: pos@0.0.0
7 verbose lifecycle pos@0.0.0~build: unsafe-perm in lifecycle true
8 verbose lifecycle pos@0.0.0~build: PATH: /usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/Users/jho/TzuChi/taurus/flaskr/static/pos/node_modules/.bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Frameworks/Mono.framework/Versions/Current/Commands
9 verbose lifecycle pos@0.0.0~build: CWD: /Users/jho/TzuChi/taurus/flaskr/static/pos
10 silly lifecycle pos@0.0.0~build: Args: [ '-c', 'ng build' ]
11 silly lifecycle pos@0.0.0~build: Returned: code: 1  signal: null
12 info lifecycle pos@0.0.0~build: Failed to exec build script
13 verbose stack Error: pos@0.0.0 build: `ng build`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:326:16)
13 verbose stack     at EventEmitter.emit (events.js:200:13)
13 verbose stack     at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:200:13)
13 verbose stack     at maybeClose (internal/child_process.js:1021:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:283:5)
14 verbose pkgid pos@0.0.0
15 verbose cwd /Users/jho/TzuChi/taurus/flaskr/static/pos
16 verbose Darwin 18.7.0
17 verbose argv "/usr/local/Cellar/node/12.4.0/bin/node"    "/usr/local/bin/npm" "run" "build"
18 verbose node v12.4.0
19 verbose npm  v6.10.2
20 error code ELIFECYCLE
21 error errno 1
22 error pos@0.0.0 build: `ng build`
22 error Exit status 1
23 error Failed at the pos@0.0.0 build script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

Thank you so much for any feedback.

Jonathan Ho
  • 41
  • 1
  • 6
  • Have a look at this [answer](https://stackoverflow.com/a/42782430/8471295) from Jack. In his answer Jack explains in-depth how type lookup works and how you make the gapi type reference behave nicely within angular. – Severin Jaeschke Sep 04 '19 at 15:40

1 Answers1

0

Turns out I was implementing the first link incorrectly. The answer was to declare the variable gapi: any outside of the service.

declare var gapi: any; //this is the key

@Injectable({
  providedIn: 'root'
})
export class UserService {

  constructor(private router: Router) { }
  ... 
}
Jonathan Ho
  • 41
  • 1
  • 6