【问题标题】:Cannot read property 'prototype' of undefined in angular无法读取角度未定义的属性“原型”
【发布时间】:2019-05-27 21:57:00
【问题描述】:

我通过 npm 在 angular(最新版本)中安装了一个包,它使用了一些库,例如“fs,stream,..”

当我运行ng serve 时,它会显示许多错误,例如:

Can't resolve 'crypto' in '\node_modules\..'
Can't resolve 'fs' in '\node_modules\..'
Can't resolve 'stream' in '\node_modules\..'

为了解决错误,我将此代码添加到 pckage.json:

"browser": {
"fs": false,
"path": false,
"os": false,
"crypto" : false,
"stream" : false,
"http" : false,
"tls" : false,
"zlib" : false,
"https" : false,
"net" : false
}

之后我看到这两个错误:

global is not defined
process is not defined

为了解决这两个问题,我将这部分代码添加到我的polyfills.ts

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

并且错误消失了,但现在我在控制台中有此错误:

TypeError: Cannot read property 'prototype' of undefined
at Object.inherits (inherits_browser.js:5)
at Object../node_modules/sshpk/lib/ed-compat.js (ed-compat.js:25)
at __webpack_require__ (bootstrap:78)
at Object../node_modules/sshpk/lib/private-key.js (private-key.js:17)
at __webpack_require__ (bootstrap:78)
at Object../node_modules/sshpk/lib/fingerprint.js (fingerprint.js:11)
at __webpack_require__ (bootstrap:78)
at Object../node_modules/sshpk/lib/key.js (key.js:8)
at __webpack_require__ (bootstrap:78)
at Object../node_modules/sshpk/lib/index.js (index.js:3)

我不知道该怎么办!

感谢您的帮助!

更多信息:

在 react 中测试的包并运行良好

包地址:https://www.npmjs.com/package/podchat

和角度组件

import { Component, OnInit  } from '@angular/core';
import * as Chat from 'podchat';

@Component({
  selector: 'app-c2',
  templateUrl: './test.component.html',
  styleUrls: ['./test.component.scss']
})
export class TestComponent implements OnInit {

  flag: boolean;
  params = {
    socketAddress: "wss://chat-sandbox.pod.land/ws", // {**REQUIRED**} Socket Address
    ssoHost: "https://accounts.pod.land", // {**REQUIRED**} Socket Address
    platformHost: "https://sandbox.pod.land:8043/srv/basic-platform", // {**REQUIRED**} Platform Core Address
    fileServer: "https://sandbox.pod.land:8443", // {**REQUIRED**} File Server Address
    serverName: "chat-server", // {**REQUIRED**} Server to to register on
    grantDeviceIdFromSSO: false,
    enableCache: true, // Enable Client side caching
    token: "80fdb34b3086438e91061d0902f33c7b", // {**REQUIRED**} SSO Token
    wsConnectionWaitTime: 500, // Time out to wait for socket to get ready after open
    connectionRetryInterval: 5000, // Time interval to retry registering device or registering server
    connectionCheckTimeout: 10000, // Socket connection live time on server
    messageTtl: 86400000, // Message time to live
    reconnectOnClose: true, // auto connect to socket after socket close
    asyncLogging: {
        onFunction: true, // log main actions on console
        onMessageReceive: true, // log received messages on console
        onMessageSend: true, // log sent messaged on console
        actualTiming: true // log actual functions running time
    }
};
  constructor() { 
  }

  ngOnInit() {
    const chat = new Chat(this.params);
  }
}

【问题讨论】:

  • 我遇到了同样的问题,因为我们关闭了散列,所以延迟加载的模块被磁盘缓存了;打开散列后它工作正常。

标签: angular npm angular7


【解决方案1】:

这个库依赖于 fs 包,这是一个 node.js 包,用于在文件系统中创建/读取/修改文件。由于您无法在浏览器中执行此操作,因此您将永远不需要这些包。您可以删除它们。

根据 github 页面,podchat 有一个客户端部分,您应该包含它:

import { Component, OnInit  } from '@angular/core';
import * as Chat from 'podchat/src/chat-browser';

我不确定要导入的正确语法,但我希望你能明白。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-10
    • 2019-04-28
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    • 2015-07-01
    相关资源
    最近更新 更多