diff --git a/packages/utils/src/node.ts b/packages/utils/src/node.ts index 18d29fc3144f..d7a58204f974 100644 --- a/packages/utils/src/node.ts +++ b/packages/utils/src/node.ts @@ -25,32 +25,17 @@ export function dynamicRequire(mod: any, request: string): any { /** * Helper for dynamically loading module that should work with linked dependencies. - * The problem is that we _should_ be using `require(require.resolve(moduleName, { paths: [cwd()] }))` + * This is equivalent of using `require(require.resolve(moduleName, { paths: [cwd()] }))` * However it's _not possible_ to do that with Webpack, as it has to know all the dependencies during - * build time. `require.resolve` is also not available in any other way, so we cannot create, - * a fake helper like we do with `dynamicRequire`. - * - * We always prefer to use local package, thus the value is not returned early from each `try/catch` block. - * That is to mimic the behavior of `require.resolve` exactly. + * build time. * * @param moduleName module name to require * @returns possibly required module */ export function loadModule(moduleName: string): T | undefined { - let mod: T | undefined; - try { - mod = dynamicRequire(module, moduleName); + return dynamicRequire(require.main, moduleName); } catch (e) { - // no-empty + return undefined; } - - try { - const { cwd } = dynamicRequire(module, 'process'); - mod = dynamicRequire(module, `${cwd()}/node_modules/${moduleName}`) as T; - } catch (e) { - // no-empty - } - - return mod; }