tRPC and WebSockets

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/xxxxx/Projects/xxxxxxx/xxxxxxx/src/env/server.mjs not supported.

Instead change the require of /home/xxxxx/Projects/xxxxxxx/xxxxxxx/src/env/server.mjs to a dynamic import() which is available in all CommonJS modules.
at Object.<anonymous> (/home/xxxxx/Projects/xxxxxxx/xxxxxxx/src/server/db/client.ts:6:22)
Error [ERR_REQUIRE_ESM]: require() of ES Module /home/xxxxx/Projects/xxxxxxx/xxxxxxx/src/env/server.mjs not supported.

Instead change the require of /home/xxxxx/Projects/xxxxxxx/xxxxxxx/src/env/server.mjs to a dynamic import() which is available in all CommonJS modules.
at Object.<anonymous> (/home/xxxxx/Projects/xxxxxxx/xxxxxxx/src/server/db/client.ts:6:22)
how can I dynamically import env from /env/server/mjs ? ts-node-dev --project tsconfig.server.json --respawn --transpile-only --ignore-watch node_modules --no-notify --exit-child src/server/wsServer.ts
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "dist",
"target": "ES2019",
"lib": ["ES2019", "DOM"],
"isolatedModules": false,
"noEmit": false
},
"include": ["next-env.d.ts", "src/**/*.ts", "src/**/*.tsx"]
}
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "dist",
"target": "ES2019",
"lib": ["ES2019", "DOM"],
"isolatedModules": false,
"noEmit": false
},
"include": ["next-env.d.ts", "src/**/*.ts", "src/**/*.tsx"]
}
import { env } from "../../env/server.mjs";
import { env } from "../../env/server.mjs";
19 Replies
HAL 9000
HAL 9000OPā€¢3y ago
I am trying to compile wsServer.ts @wsServer.ts
import ws from "ws";
import { applyWSSHandler } from '@trpc/server/adapters/ws';
import { appRouter } from "./router";
import { createContext } from "./router/context";

const wss = new ws.Server({ port: 3001 });

const handler = applyWSSHandler({
wss,
createContext,
router: appRouter,
});

wss.on('connection', (socket) => {
console.log(`++ ws connection ${wss.clients.size}`);

wss.on('close', () => {
console.log(`-- ws connection ${wss.clients.size}`);
});
});


console.log('Listening on ws://localhost:3001');

process.on('SIGTERM', () => {
console.log('SIGTERM received, closing server');

handler.broadcastReconnectNotification();

wss.close((error) => {
if (error) {
console.error(error);
process.exit(1);
}

process.exit(0);
});
});
import ws from "ws";
import { applyWSSHandler } from '@trpc/server/adapters/ws';
import { appRouter } from "./router";
import { createContext } from "./router/context";

const wss = new ws.Server({ port: 3001 });

const handler = applyWSSHandler({
wss,
createContext,
router: appRouter,
});

wss.on('connection', (socket) => {
console.log(`++ ws connection ${wss.clients.size}`);

wss.on('close', () => {
console.log(`-- ws connection ${wss.clients.size}`);
});
});


console.log('Listening on ws://localhost:3001');

process.on('SIGTERM', () => {
console.log('SIGTERM received, closing server');

handler.broadcastReconnectNotification();

wss.close((error) => {
if (error) {
console.error(error);
process.exit(1);
}

process.exit(0);
});
});
Hey @SeanCassiere any idea about this?
Sean Cassiere
Sean Cassiereā€¢3y ago
I don't think you can dynamically import your env vars. Question, is this a standard Node tRPC server? or one using NextJS?
Because you can't run Websocket servers on NextJS due it being serverless/lambdas
HAL 9000
HAL 9000OPā€¢3y ago
i believe it doesn't use next js
Sean Cassiere
Sean Cassiereā€¢3y ago
Ok, then that shouldn't be an issue then.
FYI, an easy way if checking yours is a Next project, is too if there is a next.config.js in your project root.
Other than that there's no reason it shouldn't work. Although I don't see your import statement in the code either. What's the actual issue here, maybe I'm just a bit confused šŸ¤”
HAL 9000
HAL 9000OPā€¢3y ago
it is using createContext
HAL 9000
HAL 9000OPā€¢3y ago
i am trying to follow this video
Sean Cassiere
Sean Cassiereā€¢3y ago
Normally it shouldn't affect it, that seems odd šŸ¤” Not sure what you are doing in the createContext, but that seems like a weird af bug.
HAL 9000
HAL 9000OPā€¢3y ago
but i think he is using old version of t3 create app
Sean Cassiere
Sean Cassiereā€¢3y ago
Quite possibly, and FYI that's a NextJS project.
HAL 9000
HAL 9000OPā€¢3y ago
yea i know
Sean Cassiere
Sean Cassiereā€¢3y ago
Are you running into this issue in dev, or prod btw?
HAL 9000
HAL 9000OPā€¢3y ago
dev
Sean Cassiere
Sean Cassiereā€¢3y ago
Seems quite weird. I'm unable to clone the repo and try it out or anything, as I'm out of the house, but I'm interested to see where this goes.
Unknown User
Unknown Userā€¢3y ago
Message Not Public
Sign In & Join Server To View
HAL 9000
HAL 9000OPā€¢3y ago
@flicknote I am getting Invalid Environment Variable error
āŒ Invalid environment variables:
NODE_ENV: Required

/home/xxxx/xxxx/xxxx/xxxx/src/env/server.mjs:17
throw new Error("Invalid environment variables");
^

Error: Invalid environment variables
at Object.<anonymous> (/home/xxxx/xxxx/xxxx/xxxx/src/env/server.mjs:17:9)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.v (/home/xxxx/xxxx/xxxx/xxxx/node_modules/@esbuild-kit/cjs-loader/dist/index.js:1:933)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (//home/xxxx/xxxx/xxxx/xxxx/src/server/db/client.ts:6:21)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.v (/home/xxxx/xxxx/xxxx/xxxx/node_modules/@esbuild-kit/cjs-loader/dist/index.js:1:933)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
āŒ Invalid environment variables:
NODE_ENV: Required

/home/xxxx/xxxx/xxxx/xxxx/src/env/server.mjs:17
throw new Error("Invalid environment variables");
^

Error: Invalid environment variables
at Object.<anonymous> (/home/xxxx/xxxx/xxxx/xxxx/src/env/server.mjs:17:9)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.v (/home/xxxx/xxxx/xxxx/xxxx/node_modules/@esbuild-kit/cjs-loader/dist/index.js:1:933)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (//home/xxxx/xxxx/xxxx/xxxx/src/server/db/client.ts:6:21)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.v (/home/xxxx/xxxx/xxxx/xxxx/node_modules/@esbuild-kit/cjs-loader/dist/index.js:1:933)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Unknown User
Unknown Userā€¢3y ago
Message Not Public
Sign In & Join Server To View
HAL 9000
HAL 9000OPā€¢3y ago
yea I figured when I run nextjs with yarn dev i don't actually set it manually neither yarn dev sets it (next dev sets it to dev under the hood i guess) after I added the NODE_ENV=development to .env it works now thanks
Unknown User
Unknown Userā€¢3y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server