fix(machineToken): use require() for node-machine-id to survive webpack bundling

The default import + destructuring pattern was being mangled by webpack's
static analysis during Next.js standalone builds, causing 'Cannot destructure
property machineIdSync of undefined' errors in production.

Using require() bypasses webpack's interop wrapper (c.n(...)) and loads the
module's exports directly at runtime.
This commit is contained in:
diegosouzapw
2026-05-15 12:10:36 -03:00
parent 1c949c248b
commit bb0ec76f24

View File

@@ -1,7 +1,13 @@
import { createHmac } from "node:crypto";
import nodeMachineId from "node-machine-id";
const { machineIdSync } = nodeMachineId;
// eslint-disable-next-line @typescript-eslint/no-require-imports
let machineIdSync: (original?: boolean) => string;
try {
// Use require() to bypass webpack static analysis that breaks the default export
const mod = require("node-machine-id");
machineIdSync = mod.machineIdSync || mod.default?.machineIdSync;
} catch {
machineIdSync = () => "";
}
const BUILTIN_DEFAULT_SALT = "omniroute-cli-auth-v1";