Angular 6 Uncaught ReferenceError:未定义缓冲区

Dmi*_*nko 16 angular

我试图从5迁移到6使用ng update,我得到一个错误

Uncaught ReferenceError: Buffer is not defined
    at Object../node_modules/amazon-cognito-identity-js/node_modules/crypto-browserify/helpers.js (helpers.js:2)
    at __webpack_require__ (bootstrap:81)
    at Object../node_modules/amazon-cognito-identity-js/node_modules/crypto-browserify/md5.js (md5.js:10)
    at __webpack_require__ (bootstrap:81)
    at Object../node_modules/amazon-cognito-identity-js/node_modules/crypto-browserify/create-hash.js (create-hash.js:3)
    at __webpack_require__ (bootstrap:81)
    at Object../node_modules/amazon-cognito-identity-js/node_modules/crypto-browserify/index.js (index.js:12)
    at __webpack_require__ (bootstrap:81)
    at Object../node_modules/amazon-cognito-identity-js/es/AuthenticationHelper.js (vendor.js:47207)
    at __webpack_require__ (bootstrap:81)
Run Code Online (Sandbox Code Playgroud)

本地环境适用于创建新的角度项目.我不使用Buffer.这是幕后的事情

有任何想法吗?

UPD

我试图更新@ types/node npm install --save-dev @types/node

+ @types/node@8.9.5
updated 1 package in 12.031s
[!] 26 vulnerabilities found [36141 packages audited]
    Severity: 11 Low | 13 Moderate | 2 High
    Run `npm audit` for more detail
Run Code Online (Sandbox Code Playgroud)

如果我跑 npm audit

npm ERR! code ENOAUDIT
npm ERR! audit Your configured registry (https://registry.npmjs.org/) does not support audit requests.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/myname/.npm/_logs/2018-05-16T13_45_17_410Z-debug.log
Run Code Online (Sandbox Code Playgroud)

max*_*992 37

好吧,一个小时后我终于设法让Cognito在我的Angular应用程序上工作(升级到6.0之后).

关于消息global is not defined(或关闭的东西不记得).将以下内容添加到index.html中:

<!doctype html>
<html lang="en">
<head>
  ...

  <script>
    var global = global || window;
  </script>
</head>
Run Code Online (Sandbox Code Playgroud)

然后,您可能会收到一条错误消息,指出Buffer未定义.

buffer使用npm或纱线安装包装.并将以下内容添加到polyfills.ts()中:

global.Buffer = global.Buffer || require('buffer').Buffer;
Run Code Online (Sandbox Code Playgroud)

Stackoverflow回答/ github问题帮助我,以防万一之后没有为你修复:

升级到angular-6.x会出现"Uncaught ReferenceError:global is not defined"

https://github.com/aws/aws-amplify/issues/840#issuecomment-389459988

https://github.com/aws/aws-amplify/issues/678

https://github.com/aws/aws-amplify/issues/153

https://github.com/crypto-browserify/createHash/issues/20


rgu*_*rin 16

我添加了相同的问题,试图randomBytescrypto我的 polyfill.ts 文件globalBuffer引用中运行该方法。

不幸的是,我仍然收到以下错误消息: _stream_writable.js:57 Uncaught TypeError: Cannot read property 'slice' of undefined

_stream_writable.js文件中查找后,我看到未定义的指针在一条process.version.slice指令上。

总而言之,在我的 polyfill 文件中添加以下几行完全解决了它:

(window as any).global = window;
global.Buffer = global.Buffer || require('buffer').Buffer;
(window as any).process = {
  version: ''
};
Run Code Online (Sandbox Code Playgroud)

我希望它能帮助别人...


Chr*_*ier 9

@ maxime1992
对我来说,在开始时,这也解决了我从迁移到角度6后检测到的问题,即一个声明良好的模块,但是当你想要进入其中一个视图时却找不到.
问题来自那里,而不是来自我的模块导入或其他.

但是当我启动ng test命令时,现在我有这个错误:

DeprecationWarning:不推荐使用Tapable.plugin..hooks而是使用新的API

显然,这是一个webpack问题.
因此,我更喜欢使用此解决方案:
将此行添加到polyfills.ts应解决节点全局错误

(window as any).global = window;
Run Code Online (Sandbox Code Playgroud)

再次感谢 !!!