From f7efaec80e7853643efe99c7c28e646294f4918e Mon Sep 17 00:00:00 2001 From: Bryan English Date: Tue, 23 Sep 2025 14:07:54 -0400 Subject: [PATCH] fix: use createRequire to load hook.js If import is used to load hook.js, then any loaders that assign `result.source` (as indicated by the Node.js docs) on CommonJS files will cause a not-implemented error to be thrown inside Node.js. This is probably a bug in Node.js, but this ought to be an appropriate workaround (espectially since IITM supports old versions of Node.js). --- hook.mjs | 4 +++- test/fixtures/double-loader.mjs | 9 +++++++++ test/other/double-loading.mjs | 15 +++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/double-loader.mjs create mode 100644 test/other/double-loading.mjs diff --git a/hook.mjs b/hook.mjs index 37acec3..ce44316 100644 --- a/hook.mjs +++ b/hook.mjs @@ -2,7 +2,9 @@ // // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc. -import { createHook } from './hook.js' +import { createRequire } from 'node:module' +const require = createRequire(import.meta.url) +const { createHook } = require('./hook.js') const { initialize, load, resolve, getFormat, getSource } = createHook(import.meta) diff --git a/test/fixtures/double-loader.mjs b/test/fixtures/double-loader.mjs new file mode 100644 index 0000000..8b85c2d --- /dev/null +++ b/test/fixtures/double-loader.mjs @@ -0,0 +1,9 @@ +import { readFile } from 'fs/promises' + +export async function load (url, context, nextLoad) { + const result = await nextLoad(url, context) + if (!result.source && url.startsWith('file:')) { + result.source = await readFile(new URL(url)) + } + return result +} diff --git a/test/other/double-loading.mjs b/test/other/double-loading.mjs new file mode 100644 index 0000000..48e3ca0 --- /dev/null +++ b/test/other/double-loading.mjs @@ -0,0 +1,15 @@ +// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2.0 License. +// +// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc. + +import { execSync } from 'child_process' +import { doesNotThrow } from 'assert' + +const env = { + ...process.env, + NODE_OPTIONS: '--no-warnings --experimental-loader ./test/fixtures/double-loader.mjs --experimental-loader ./hook.mjs' +} + +doesNotThrow(() => { + execSync('node -p 0', { env }) +})