node/test/module-hooks/test-module-hooks-load-import-cjs.js
Joyee Cheung f4646a7f4b
module: do not invoke resolve hooks twice for imported cjs
Previously the resolve hook can be invoked twice from the
synthetic module evaluation step of imported CJS in the extra
module._load() call that's invoked on the resolved full path.
Add an option to avoid it, since the resolution and loading
has already been done before.

PR-URL: https://github.com/nodejs/node/pull/61529
Fixes: https://github.com/nodejs/node/issues/57125
Refs: https://github.com/nodejs/node/issues/55808
Refs: https://github.com/nodejs/node/issues/56241
Reviewed-By: Jacob Smith <jacob@frende.me>
2026-02-02 23:04:23 +00:00

20 lines
636 B
JavaScript

'use strict';
// Test that load hook in imported CJS only gets invoked once.
const common = require('../common');
const assert = require('assert');
const { registerHooks } = require('module');
const path = require('path');
const { pathToFileURL } = require('url');
const hook = registerHooks({
load: common.mustCall(function(url, context, nextLoad) {
assert.strictEqual(url, pathToFileURL(path.resolve(__dirname, '../fixtures/value.cjs')).href);
return nextLoad(url, context);
}, 1),
});
import('../fixtures/value.cjs').then(common.mustCall((result) => {
assert.strictEqual(result.value, 42);
hook.deregister();
}));