HookWebpackError: __name is not defined
Unanswered
Derock posted this in #help-forum
DerockOP
Hello, I am trying to add a custom server to my project for websocket support, but whenever I run the server using the custom server, I get the following error:
My custom server code:
I am executing with
â—‹ Compiling /page ...
⨯ ./src/styles/globals.css
HookWebpackError: __name is not defined
-- inner error --
ReferenceError: __name is not defined
Generated code for webpack/runtime/hot module replacement
1 | ... truncated, see https://derock.media/s31JDE.txt ...
Import trace for requested module:
./src/styles/globals.css
My custom server code:
// src/server/server.ts
import "dotenv/config";
import next from "next";
import { env } from "~/env.mjs";
import { createServer } from "http";
import logger from "./utils/logger";
import ws from "ws";
import { applyWSSHandler } from "@trpc/server/adapters/ws";
import { appRouter } from "./api/root";
import { createTRPCContext } from "./api/trpc";
import { incomingRequestToNextRequest } from "./utils/serverUtils";
async function startApp() {
// initialize the next app
const app = next({
dev: env.NODE_ENV !== "production",
hostname: env.HOSTNAME,
port: env.PORT,
});
await app.prepare();
// get the handles
const getHandler = app.getRequestHandler();
const upgradeHandler = app.getUpgradeHandler();
// create the http server
const server = createServer((req, res) => {
getHandler(req, res).catch((error) => {
logger.error(error);
res.statusCode = 500;
res.end("Internal Server Error");
});
});
// create the websocket server
const wss = new ws.Server({ noServer: true });
const trpcHandler = applyWSSHandler({
wss,
router: appRouter,
createContext: ({ req }) =>
createTRPCContext({
req: incomingRequestToNextRequest(req),
resHeaders: new Headers(),
}),
});
process.on("SIGTERM", () => {
logger.warn("SIGTERM received, shutting down...");
trpcHandler.broadcastReconnectNotification();
server.close(() => {
process.exit(0);
});
});
// handle the upgrade
server.on("upgrade", (req, socket, head) => {
// send trpc requests to the trpc server
if (req.url?.startsWith("/api/trpc")) {
wss.handleUpgrade(req, socket, head, () => undefined);
} else {
void upgradeHandler(req, socket, head);
}
});
// start the server
server.listen(env.PORT, () => {
logger.info(`Server listening on port ${env.PORT}`);
});
}
void startApp();
I am executing with
pnpm exec tsx watch src/server/server.ts --project tsconfig.server.json
// tsconfig.server.json
{
"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"]
}
6 Replies
DerockOP
version
v14.0.1
issue also occurs in next@canary
DerockOP
and removing my import for the css changes where the error occurs,
An error occured in `next/font`.
HookWebpackError: __name is not defined
these seems to be an issue with
tsx
, running a plain js version works fine.DerockOP
struggling to find a better alternative here
pnpm ts-node --project tsconfig.server.json -r tsconfig-paths/register ./src/server/server.ts
errors with
Error [ERR_REQUIRE_ESM]: require() of ES Module /home/derock/Documents/Code/hostforge/node_modules/.pnpm/superjson@2.2.0/node_modules/superjson/dist/index.js from /home/derock/Documents/Code/hostforge/src/server/api/trpc.ts not supported.
if I try to add
"type": "module"
to my package.json, ts-node
completely breaks.trying
node --experimental-specifier-resolution=node --experimental-modules --no-warnings --loader ts-node/esm -r tsconfig-paths/register ./src/server/server.ts
doesn't work because i'm using the paths
field in tsconfig:node:internal/process/esm_loader:40
internalBinding('errors').triggerUncaughtException(
^
Error: Cannot find package '~' imported from /home/derock/Documents/Code/hostforge/src/server/server.ts
and tsconfig-paths doesnt work with esm.
if i use my own loader, I get
node --experimental-specifier-resolution=node --experimental-modules --no-warnings --loader ts-node/esm -r loader.js ./src/server/server.ts
node:internal/modules/cjs/loader:1051
throw err;
^
Error: Cannot find module 'loader.js'
Require stack:
- internal/preload
at Module._resolveFilename (node:internal/modules/cjs/loader:1048:15)
at Module._load (node:internal/modules/cjs/loader:901:27)
at internalRequire (node:internal/modules/cjs/loader:166:19)
at Module._preloadModules (node:internal/modules/cjs/loader:1420:5)
at loadPreloadModules (node:internal/process/pre_execution:705:5)
at setupUserModules (node:internal/process/pre_execution:170:3)
at prepareExecution (node:internal/process/pre_execution:132:5)
at prepareMainThreadExecution (node:internal/process/pre_execution:55:3)
at node:internal/main/run_main_module:10:1 {
code: 'MODULE_NOT_FOUND',
requireStack: [ 'internal/preload' ]
}
Node.js v20.9.0