Skip to content

Commit 70a2a65

Browse files
[backport v2] createhash: only call module.require if it exists (#466) (#468)
Apparently Next.JS "Turbopack" runs code in a weird semi-Node environment that doesn't have module.require. We don't want to call require directly because that makes other bundlers put in polyfills. So make Turbopack use sha.js instead of just throwing. Non-ideal but better than what we have today. For apollographql/apollo-server#8004 Co-authored-by: David Glasser <[email protected]>
1 parent 5534029 commit 70a2a65

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

.changeset/early-boxes-check.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@apollo/utils.createhash": patch
3+
---
4+
5+
Compatibility with Next.js Turbopack

packages/createHash/src/index.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { isNodeLike } from "@apollo/utils.isnodelike";
22

33
export function createHash(kind: string): import("crypto").Hash {
4-
if (isNodeLike) {
4+
// Some Node-like environments (like next.js Turbopack) apparently
5+
// don't have module.require, so double-check before we call it.
6+
// (But don't change the value of isNodeLike because other logic depends on it,
7+
// like Apollo Server signal handling defaults.) This does mean that
8+
// Turbopack will call sha.js instead of the native crypto module, but
9+
// it sure beats throwing because module.require does not exist.
10+
if (isNodeLike && module.require) {
511
// Use module.require instead of just require to avoid bundling whatever
612
// crypto polyfills a non-Node bundler might fall back to.
713
return module.require("crypto").createHash(kind);

0 commit comments

Comments
 (0)