aws-amplify js 到 Angular 应用程序有错误:未定义全局
Posted
技术标签:
【中文标题】aws-amplify js 到 Angular 应用程序有错误:未定义全局【英文标题】:aws-amplify js to angular app has error: global is not defined 【发布时间】: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)
有什么想法吗?
【问题讨论】:
【参考方案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 等等等等。谢谢。以上是关于aws-amplify js 到 Angular 应用程序有错误:未定义全局的主要内容,如果未能解决你的问题,请参考以下文章
尝试将我的“aws-amplify”包导入我的入口点时收到 JSLint 错误
在 React 中使用 aws-amplify 将文件上传到具有联合身份的 S3 时出错
AWS-amplify 在请求中包含 cognito Authorization 标头