beewest
beewest

Reputation: 4846

aws-amplify js to angular app has error: global is not defined

I am testing AWS Amplify from Angular to Cognito User Pool following:

https://docs.amplify.aws/lib/restapi/getting-started/q/platform/js

The Angular app has successfully compiled however the exception throws out in the Chrome console:

index.js:43 Uncaught ReferenceError: global is not defined
at Object../node_modules/buffer/index.js (index.js:43)
at __webpack_require__ (bootstrap:84)
at Module../node_modules/amazon-cognito-identity-js/es/AuthenticationHelper.js (AuthenticationHelper.js:1)
at __webpack_require__ (bootstrap:84)
at Module../node_modules/amazon-cognito-identity-js/es/index.js (index.js:1)
at __webpack_require__ (bootstrap:84)
at Module../node_modules/@aws-amplify/auth/lib-esm/Auth.js (Auth.js:1)
at __webpack_require__ (bootstrap:84)
at Module../node_modules/@aws-amplify/auth/lib-esm/index.js (index.js:1)
at __webpack_require__ (bootstrap:84)

ANy idea please?

Upvotes: 17

Views: 7914

Answers (4)

Dinesh
Dinesh

Reputation: 1810

In my case, it was failing when running npm test. So I had to add the following to test.ts after the imports.

test.ts

(window as any).global = window;

Upvotes: 1

glenn
glenn

Reputation: 289

The AWS docs suggest adding

(window as any).global = window;

(window as any).process = {
  env: { DEBUG: undefined },
};

to src/polyfills.ts

This has worked in my projects.

https://docs.amplify.aws/start/getting-started/data-model/q/integration/ionic#connect-frontend-to-api

Upvotes: 14

Manoj Alwis
Manoj Alwis

Reputation: 1426

I had a chance to fix this issue by adding the below code to index.html file header section.

<head>
<script>
  if (global === undefined) {
    var global = window;
  }
</script>
</head>

Upvotes: 4

beewest
beewest

Reputation: 4846

this suggestion works in this case as well.

<script>
    var global = global || window;
    var Buffer = Buffer || [];
    var process = process || {
      env: { DEBUG: undefined },
      version: []
    };
  </script>

or add to the end of polyfills.ts

(window as any).global = window;
(window as any).process = {
  env: { DEBUG: undefined },
};

Upvotes: 25

Related Questions