沉冰浮水

沉冰浮水

做最终到的事,成为最终成为的人!
github
bilibili
mastodon
zhihu
douban

「大坑」关于 module 模式下如何正确引入 WebSocketServer

WebSocket.Server is not a constructor

遇到上边错误,直接照着 cjs 写法改成 mjs 形式,但是用不了;

import WebSocket from "ws"
console.log(typeof WebSocket)
console.log(typeof WebSocket.Server)
// function
// undefined

用报错的「WebSocket.Server is not a constructor」作为关键词并没能找到原因;

直到我换了个姿势,直接搜索「import WebSocket from 'ws'」,得到的代码是下边这样子的:

import WebSocket, { WebSocketServer } from 'ws';
console.log(typeof WebSocket)
console.log(typeof WebSocketServer)
// function
// function

然后我去看了 ws 这个库的导出文件;

// cjs
'use strict';

const WebSocket = require('./lib/websocket');

WebSocket.createWebSocketStream = require('./lib/stream');
WebSocket.Server = require('./lib/websocket-server');
WebSocket.Receiver = require('./lib/receiver');
WebSocket.Sender = require('./lib/sender');

WebSocket.WebSocket = WebSocket;
WebSocket.WebSocketServer = WebSocket.Server;

module.exports = WebSocket;
// mjs
import createWebSocketStream from './lib/stream.js';
import Receiver from './lib/receiver.js';
import Sender from './lib/sender.js';
import WebSocket from './lib/websocket.js';
import WebSocketServer from './lib/websocket-server.js';

export { createWebSocketStream, Receiver, Sender, WebSocket, WebSocketServer };
export default WebSocket;

差别不只是有点儿大好么 Orz;

加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。