Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular + amazon-cognito-identity-js, why am I getting error: global is not defined

From a command line:

ng new sandbox
cd .\sandbox\
ng serve

Open http://localhost:4200/. It works.

npm install --save amazon-cognito-identity-js

Inside \src\app\sign-up\sign-up.component.ts add this line of code:

import * as AmazonCognitoIdentity from 'amazon-cognito-identity-js';

Add a constructor:

  constructor() {
    new AmazonCognitoIdentity.CognitoUserPool({});
  }

Refresh http://localhost:4200/.

Page is blank. There is a console error:

Uncaught ReferenceError: global is not defined
    at Object../node_modules/buffer/index.js (index.js:43)
    at __webpack_require__ (bootstrap:76)
    at Object../node_modules/amazon-cognito-identity-js/es/AuthenticationHelper.js (AuthenticationHelper.js:1)
    at __webpack_require__ (bootstrap:76)
    at Object../node_modules/amazon-cognito-identity-js/es/index.js (index.js:1)
    at __webpack_require__ (bootstrap:76)
    at Object../src/app/app.component.ts (main.js:94)
    at __webpack_require__ (bootstrap:76)
    at Object../src/app/app.module.ts (app.component.ts:9)
    at __webpack_require__ (bootstrap:76)

What is the best way to resolve this error?

like image 611
Jesus is Lord Avatar asked Dec 08 '22 14:12

Jesus is Lord


1 Answers

Put this as the last line in /src/polyfills.ts

(window as any).global = window
like image 199
danday74 Avatar answered Dec 28 '22 08:12

danday74