Angular 6 Uncaught ReferenceError: Buffer is not defined

问题描述 投票:0回答:10

我正在尝试使用

ng update
从 5 迁移到 6,但出现错误

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)

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

有什么想法吗?

更新

我正在尝试更新@types/node

npm install --save-dev @types/node

+ @types/[email protected]
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

如果我跑

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
angular
10个回答
85
投票

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

关于消息

global is not defined
(或一些不记得的东西)。将以下内容添加到index.html

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

  <script>
    var global = global || window;
  </script>
</head>

然后,你可能会得到一个错误,说 Buffer is not defined.

使用 npm 或 yarn 安装

buffer
包。并将以下内容添加到 polyfills.ts ():

global.Buffer = global.Buffer || require('buffer').Buffer;

Stackoverflow answers/github issues 对我有帮助,以防它在那之后没有为你修复:

升级到 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


28
投票

我添加了同样的问题,试图从

randomBytes
运行
crypto
方法,并在我的 polyfill.ts 文件中添加了
global
Buffer
引用。

不幸的是,我仍然有以下错误信息:

_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: ''
};

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


11
投票

@maxime1992
对我来说,一开始,这也解决了我在 迁移到 angular 6 后发现的一个问题,即一个状态良好的模块,但当你想继续其中一个视图时却找不到。
问题来自那里,而不是来自我的模块导入或其他。

但是当我启动

ng test
命令时,现在我有这个错误:

弃用警告:Tapable.plugin 已弃用。使用新的 API

.hooks
代替

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

(window as any).global = window;

再次感谢!!!


4
投票

我使用

polyfill.ts
文件中的这些声明解决了这个问题。 在这个issue中提到了它,我也为其他问题添加了必要的声明。

import * as process from 'process';
(window as any).process = process;
(window as any)['global'] = window;
global.Buffer = global.Buffer || require('buffer').Buffer;

3
投票

为我解决的是在我的 polyfills.ts 中包含以下内容:
(需要 @ts-ignore 来防止 TS2591 错误:找不到名称“require”)

// @ts-ignore
window.Buffer = window.Buffer || require('buffer').Buffer;

参见:https://github.com/agoncal/swagger-ui-angular6/issues/2#issuecomment-435307286


2
投票

在 Angular 7 应用程序中,解决方法(https://github.com/angular/angular-cli/issues/9827#issuecomment-386154063)仅在我输入时对我有效

(window as any).global = window;

在.ts文件中并将其导入polyfill.ts,例如

import 'cstm-polyfill.ts'; 

0
投票

这是 Angular CLI 行为发生变化的结果。我最终通过使用以下补丁解决了这个问题:

https://gist.github.com/niespodd/1fa82da6f8c901d1c33d2fcbb762947d

有关更多上下文,请参见此处:

https://github.com/angular/angular-cli/issues/1548


0
投票

除了已接受的答案和其他好的回复外,我还需要将 typings.d.ts 添加到我的 /src 文件夹并在其中键入以下内容:

declare var global: any;
declare var require: any;

0
投票

如果您使用的是 Angular 13、15+ 只需在组件中导入包即可。

import { Buffer } from 'buffer';

包本身明确指出:

虽然 Buffer 类在全局范围内可用,但它是 仍然建议通过 import 或 require 语句明确引用它

随心所欲地使用它

Buffer.from(string).toString(b64);

-4
投票

如果您收到此错误,然后使用上面建议的方法解决,但随后会遇到另一个错误,说明以下内容:

Uncaught ReferenceError: process is not defined

很可能是您在应用程序中错误地使用了 HttpClient。确保您在 app.module 中使用 HttpClientModule 导入,而不是不小心使用 HttpClient 作为提供者。

是的:

  imports: [
    HttpClientModule,
  ],

否:

providers: [HttpClient],
© www.soinside.com 2019 - 2024. All rights reserved.