【问题标题】:aws-amplify js to angular app has error: global is not definedaws-amplify js 到 Angular 应用程序有错误:未定义全局
【发布时间】:2020-08-16 16:44:04
【问题描述】:

我正在测试从 Angular 到 Cognito 用户池的 AWS Amplify:

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

Angular 应用已成功编译,但在 Chrome 控制台中抛出异常:

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)

有什么想法吗?

【问题讨论】:

    标签: angular amazon-cognito aws-amplify


    【解决方案1】:

    this suggestion works in this case as well.

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

    或添加到polyfills.ts的末尾

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

    【讨论】:

      【解决方案2】:

      AWS 文档建议添加

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

      src/polyfills.ts

      这在我的项目中有效。

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

      【讨论】:

      • 我在 svelte-kit 中也遇到了这个错误
      【解决方案3】:

      我有机会通过将以下代码添加到 index.html 文件头部分来解决此问题。

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

      【讨论】:

      • 伙伴,非常感谢。但是,你知道为什么会这样吗?放大 - vanilla JS - Node.js 等等等等。谢谢。
      猜你喜欢
      • 2022-10-01
      • 1970-01-01
      • 2019-02-10
      • 2019-04-19
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 2020-12-16
      • 2022-01-26
      相关资源
      最近更新 更多