socket.io-client@0.9.7 带有 typescript 和 webpack2 导入错误

xso*_*ong 2 socket.io typescript webpack-2

ERROR in ./~/socket.io-client/bin/builder.js
Module not found: Error: Can't resolve 'fs' in 'E:\work\brickman2\node_modules\socket.io-client\bin'
@ ./~/socket.io-client/bin/builder.js 11:9-22
@ ./~/socket.io-client/lib/io.js
@ ./src/services/pusher.ts
@ multi main

ERROR in ./~/xmlhttprequest/lib/XMLHttpRequest.js
Module not found: Error: Can't resolve 'fs' in 'E:\work\brickman2\node_modules\xmlhttprequest\lib'
@ ./~/xmlhttprequest/lib/XMLHttpRequest.js 16:9-22
@ ./~/socket.io-client/lib/util.js
@ ./~/socket.io-client/lib/io.js
@ ./src/services/pusher.ts
@ multi main

ERROR in ./~/xmlhttprequest/lib/XMLHttpRequest.js
Module not found: Error: Can't resolve 'child_process' in 'E:\work\brickman2\node_modules\xmlhttprequest\lib'
@ ./~/xmlhttprequest/lib/XMLHttpRequest.js 15:12-36
@ ./~/socket.io-client/lib/util.js
@ ./~/socket.io-client/lib/io.js
@ ./src/services/pusher.ts
@ multi main
Run Code Online (Sandbox Code Playgroud)

我用 google 和 github issus 进行了搜索,但没有人为我工作。

socket.io-client@0.9.7 是旧版本,但无法更新。因为服务器中的socket.io版本是0.9.7并且无法更改。如果我更新客户端将无法连接服务器。

小智 5

我遇到了同样的问题,并找到了下一个解决方案 - 将需要的 socket-io.client 替换为它的捆绑版本。

ES5版本

//var io = require('socket.io-client');
var io = require('socket.io-client/dist/socket.io.js'); //worked solution
Run Code Online (Sandbox Code Playgroud)

ES6版本

//import io from 'socket.io-client';
import io from 'socket.io-client/dist/socket.io.js'; //worked solution
Run Code Online (Sandbox Code Playgroud)