From 4506e8f5dd6e96a04ee3266a08cbad24da510acc Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Mon, 15 Apr 2024 13:12:55 +0200 Subject: [PATCH] fix: Don't use `require` to load `package.json` files (#4593) * fix: Don't use require to load package.json files * update changelog * Move changelog entry --------- Co-authored-by: Marc Pichler --- experimental/CHANGELOG.md | 1 + .../src/platform/node/instrumentation.ts | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 48dc912ca28..c2ad7bcaed9 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -20,6 +20,7 @@ All notable changes to experimental packages in this project will be documented ### :bug: (Bug Fix) * fix(otlp-grpc-exporter-base): avoid TypeError on exporter shutdown [#4612](https://github.com/open-telemetry/opentelemetry-js/pull/4612) +* fix(instrumentation): Don't use `require` to load `package.json` files ### :books: (Refine Doc) diff --git a/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts b/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts index c639bc8bd48..2fe151a1fed 100644 --- a/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts +++ b/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts @@ -30,6 +30,7 @@ import { InstrumentationModuleDefinition } from '../../types'; import { diag } from '@opentelemetry/api'; import type { OnRequireFn } from 'require-in-the-middle'; import { Hook } from 'require-in-the-middle'; +import { readFileSync } from 'fs'; /** * Base abstract class for instrumenting node plugins @@ -160,8 +161,10 @@ export abstract class InstrumentationBase private _extractPackageVersion(baseDir: string): string | undefined { try { - // eslint-disable-next-line @typescript-eslint/no-var-requires - const version = require(path.join(baseDir, 'package.json')).version; + const json = readFileSync(path.join(baseDir, 'package.json'), { + encoding: 'utf8', + }); + const version = JSON.parse(json).version; return typeof version === 'string' ? version : undefined; } catch (error) { diag.warn('Failed extracting version', baseDir);