diff --git a/lib/instrumentation.js b/lib/instrumentation.js index dba0a482..6599dc38 100644 --- a/lib/instrumentation.js +++ b/lib/instrumentation.js @@ -71,7 +71,7 @@ function getPackageVersion(request, options) { } const instrumentLoad = (exports.instrumentLoad = (mod, loadRequest, parent, opts = {}) => { - if (parent.id.includes(`node_modules/${pkg.name}`) || !enabledInstrumentations.has(loadRequest)) { + if (!parent || parent.id.includes(`node_modules/${pkg.name}`) || !enabledInstrumentations.has(loadRequest)) { // no magic here return mod; } diff --git a/lib/instrumentation.test.js b/lib/instrumentation.test.js index 03048e28..c435c620 100644 --- a/lib/instrumentation.test.js +++ b/lib/instrumentation.test.js @@ -102,3 +102,11 @@ test("we keep track of the active instrumentations in a sorted array", () => { // activeInstrumentations now includes child_process expect(instrumentation.activeInstrumentations()).toEqual(["child_process", "express"]); }); + +test("don't instrument module if parent is null", () => { + instrumentation.clearInstrumentationForTesting(); + let fakeExpress = createFakeExpress(); + let m = instrumentation.instrumentLoad(fakeExpress, "express", null); + expect(m).toBe(fakeExpress); + expect(m.__wrapped).toBeUndefined(); +});