using:
t3-turbo
pnpm
bare with me please, i'm not a professional. i'm current working on a websocket server for a live messaging feature but i'm stuck at running the websocket server in ./packages/websocket having and error with the module type. i'm just following this youtube tutorial: https://www.youtube.com/watch?v=dXRRY37MPuk i did my best to fix this error but i really stuck.
files:
// packages/websocket
index.ts
package.json
tsconfig.json
index.ts, here i initialize the websocket server
import { applyWSSHandler } from "@trpc/server/adapters/ws";
import ws from "ws";
import { appRouter, createTRPCContext } from "@acme/api";
const wss = new ws.Server({
port: 3001,
});
const handler = applyWSSHandler({
wss,
router: appRouter,
createContext: createTRPCContext,
});
wss.on("connection", () => {
console.log(`Got a connection ${wss.clients.size}`);
wss.once("close", () => {
console.log(`Closed connection ${wss.clients.size}`);
});
});
console.log(`wss server start at ws://localhost:3001`);
process.on("SIGTERM", () => {
console.log("Got SIGTERM");
handler.broadcastReconnectNotification();
wss.close();
});
package.json
{
"name": "@acme/websocket",
"version": "0.1.0",
"main": "./index.ts",
"types": "./index.ts",
"license": "MIT",
"scripts": {
"dev": "ts-node-dev --project tsconfig.json --respawn --transpile-only index.ts"
},
"dependencies": {
"@acme/api": "^0.1.0",
"@trpc/client": "^10.34.0",
"@trpc/server": "^10.34.0",
"ws": "^8.13.0"
},
"devDependencies": {
"@acme/eslint-config": "^0.1.0",
"@types/ws": "^8.5.5",
"eslint": "^8.44.0",
"ts-node-dev": "^2.0.0",
"typescript": "^5.1.6"
}
}
tsconfig.json
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"module": "CommonJS",
"outDir": "dist",
"target": "ES2019",
"lib": ["ES2019", "DOM"],
"isolatedModules": false,
"noEmit": false,
"esModuleInterop": true
},
"include": ["*.ts", "*.mjs"]
}
Error:
[ERROR] 02:03:06 Error: require() of ES Module packages\auth\env.mjs not supported.
Instead change the require of packages\auth\env.mjs to a dynamic import() which is available in all CommonJS modules.