这个错误是因为您使用了ES模块的语法importrequire在同一个文件中。在Node.js中,只能在CommonJS模块中使用require,在ES模块中只能使用import

根据错误提示,您可以尝试将文件扩展名改为.cjs,或者将package.json中的"type"字段改为"commonjs",以将文件识别为CommonJS模块。

修改之后的代码如下:

const WebSocket = require('ws');

// rest of your code

如果您想继续使用ES模块的语法,可以将代码改写为:

import WebSocket from 'ws';

// rest of your code

请注意,使用import语法需要在Node.js版本12及以上。如果您的Node.js版本低于12,可以考虑升级Node.js或者继续使用require语法。

const WebSocket = requirews; ^ReferenceError require is not defined in ES module scope you can use import insteadThis file is being treated as an ES module because it has a js file ex

原文地址: https://www.cveoy.top/t/topic/h03b 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录