简体   繁体   English

Angular 中的 socket.io-stream 导入问题 6

[英]Issue with socket.io-stream importation in Angular 6

I'm using npm install socket.io-stream I implemented socket.io-stream on my angular component like this:我正在使用npm install socket.io-stream我在我的 angular 组件上实现了socket.io-stream ,如下所示:

import * as io from 'socket.io-client';
import * as ss from 'socket.io-stream';

I just want to create a duplex stream like this:我只想像这样创建一个双工 stream:

stream=ss.createStream();

I have this error when I run ng build :运行ng build时出现此错误:

ERROR in ./node_modules/socket.io-stream/lib/iostream.js
Module not found: Error: Can't resolve 'stream' in ' 
'C:\Users\geoffroy\Documents\Mines Alès\2A\Stage\WebService based 
GUI\WebApp\node_modules\socket.io-stream\lib'
ERROR in ./node_modules/socket.io-stream/lib/blob-read-stream.js
Module not found: Error: Can't resolve 'stream' in 
'C:\Users\geoffroy\Documents\Mines Alès\2A\Stage\WebService based 
GUI\WebApp\node_modules\socket.io-stream\lib'

I don't understand because on my server.js It seems to work..我不明白,因为在我的 server.js 上它似乎工作..

I try to run the command npm install stream and I don't have error with ng build .我尝试运行命令npm install stream并且ng build没有错误。 But when I launch my application I have a new error in my browser console:但是当我启动我的应用程序时,我的浏览器控制台出现了一个新错误:

inherits_browser.js:5 Uncaught TypeError: Cannot read property 'prototype' of undefined
at Object.inherits (inherits_browser.js:5)
at Object../node_modules/socket.io-stream/lib/iostream.js (iostream.js:10)
at __webpack_require__ (bootstrap:76)
at Object../node_modules/socket.io-stream/lib/socket.js (socket.js:4)
at __webpack_require__ (bootstrap:76)
at Object../node_modules/socket.io-stream/lib/index.js (index.js:1)
at __webpack_require__ (bootstrap:76)
at Object../node_modules/socket.io-stream/index.js (index.js:2)
at __webpack_require__ (bootstrap:76)

Thanks for your help谢谢你的帮助

It is a bit of old question.这是一个有点老的问题。 For me, I got the error message below:对我来说,我收到以下错误消息:

ERROR in ./node_modules/socket.io-stream/lib/blob-read-stream.js 2:15-41
Module not found: Error: Can't resolve 'stream' in 'xxx/node_modules/socket.io-stream/lib'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
    - add a fallback 'resolve.fallback: { "stream": require.resolve("stream-browserify") }'
    - install 'stream-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "stream": false }

So, I just add resolve.fallback to webpack like below.所以,我只是将 resolve.fallback 添加到 webpack,如下所示。

config.resolve = {
    modules: ['node_modules'],
    extensions: ['.ts', '.js'],
    fallback: {
      "stream": require.resolve("stream-browserify")
    },
  };

And, I installed the stream-browserify like:而且,我像这样安装了 stream-browserify:

$ npm install stream-browserify --save

Then, the issue was solved.然后,问题就解决了。

只需在 polyfills.ts 中添加这一行

// Add global to window, assigning the value of window itself. (window as any).global = window;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM