Next.js Discord

Discord Forum

HookWebpackError: __name is not defined

Derock posted this in #help-forum
Open in Discord
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:
 â—‹ 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 ...

Import trace for requested module:

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) => {
      res.statusCode = 500;
      res.end("Internal Server Error");

  // create the websocket server
  const wss = new ws.Server({ noServer: true });
  const trpcHandler = applyWSSHandler({
    router: appRouter,
    createContext: ({ req }) =>
        req: incomingRequestToNextRequest(req),
        resHeaders: new Headers(),

  process.on("SIGTERM", () => {
    logger.warn("SIGTERM received, shutting down...");
    server.close(() => {

  // 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, () => {`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

version v14.0.1
issue also occurs in next@canary
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.
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:
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 
  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 {
  requireStack: [ 'internal/preload' ]

Node.js v20.9.0