diff --git a/packages/datadog-esbuild/index.js b/packages/datadog-esbuild/index.js index ce263799023..71115075b81 100644 --- a/packages/datadog-esbuild/index.js +++ b/packages/datadog-esbuild/index.js @@ -96,7 +96,8 @@ module.exports.setup = function (build) { let pathToPackageJson try { - pathToPackageJson = require.resolve(`${extracted.pkg}/package.json`, { paths: [args.resolveDir] }) + pathToPackageJson = require.resolve(extracted.pkg, { paths: [args.resolveDir] }) + pathToPackageJson = extractPackageAndModulePath(pathToPackageJson).pkgJson } catch (err) { if (err.code === 'MODULE_NOT_FOUND') { if (!internal) { @@ -111,7 +112,7 @@ module.exports.setup = function (build) { } } - const packageJson = require(pathToPackageJson) + const packageJson = JSON.parse(fs.readFileSync(pathToPackageJson).toString()) if (DEBUG) console.log(`RESOLVE: ${args.path}@${packageJson.version}`) diff --git a/packages/datadog-instrumentations/src/utils/src/extract-package-and-module-path.js b/packages/datadog-instrumentations/src/utils/src/extract-package-and-module-path.js index 176c3c618ff..7a48565e379 100644 --- a/packages/datadog-instrumentations/src/utils/src/extract-package-and-module-path.js +++ b/packages/datadog-instrumentations/src/utils/src/extract-package-and-module-path.js @@ -6,7 +6,7 @@ const NM = 'node_modules/' * For a given full path to a module, * return the package name it belongs to and the local path to the module * input: '/foo/node_modules/@co/stuff/foo/bar/baz.js' - * output: { pkg: '@co/stuff', path: 'foo/bar/baz.js' } + * output: { pkg: '@co/stuff', path: 'foo/bar/baz.js', pkgJson: '/foo/node_modules/@co/stuff/package.json' } */ module.exports = function extractPackageAndModulePath (fullPath) { const nm = fullPath.lastIndexOf(NM) @@ -17,17 +17,20 @@ module.exports = function extractPackageAndModulePath (fullPath) { const subPath = fullPath.substring(nm + NM.length) const firstSlash = subPath.indexOf('/') + const firstPath = fullPath.substring(fullPath[0], nm + NM.length) + if (subPath[0] === '@') { const secondSlash = subPath.substring(firstSlash + 1).indexOf('/') - return { pkg: subPath.substring(0, firstSlash + 1 + secondSlash), - path: subPath.substring(firstSlash + 1 + secondSlash + 1) + path: subPath.substring(firstSlash + 1 + secondSlash + 1), + pkgJson: firstPath + subPath.substring(0, firstSlash + 1 + secondSlash) + '/package.json' } } return { pkg: subPath.substring(0, firstSlash), - path: subPath.substring(firstSlash + 1) + path: subPath.substring(firstSlash + 1), + pkgJson: firstPath + subPath.substring(0, firstSlash) + '/package.json' } }