diff --git a/.changeset/early-boxes-check.md b/.changeset/early-boxes-check.md new file mode 100644 index 00000000..de83398a --- /dev/null +++ b/.changeset/early-boxes-check.md @@ -0,0 +1,5 @@ +--- +"@apollo/utils.createhash": patch +--- + +Compatibility with Next.js Turbopack diff --git a/packages/createHash/src/index.ts b/packages/createHash/src/index.ts index c9dc1ce0..4b127082 100644 --- a/packages/createHash/src/index.ts +++ b/packages/createHash/src/index.ts @@ -1,7 +1,13 @@ import { isNodeLike } from "@apollo/utils.isnodelike"; export function createHash(kind: string): import("crypto").Hash { - if (isNodeLike) { + // Some Node-like environments (like next.js Turbopack) apparently + // don't have module.require, so double-check before we call it. + // (But don't change the value of isNodeLike because other logic depends on it, + // like Apollo Server signal handling defaults.) This does mean that + // Turbopack will call sha.js instead of the native crypto module, but + // it sure beats throwing because module.require does not exist. + if (isNodeLike && module.require) { // Use module.require instead of just require to avoid bundling whatever // crypto polyfills a non-Node bundler might fall back to. return module.require("crypto").createHash(kind);