From 4032e50915947875a21078c0a530c50a602eb12c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Nikoli=C4=87?= Date: Wed, 20 Nov 2024 19:46:08 +0100 Subject: [PATCH] Improve global require detection for path resolver --- index.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 3db554f..b646c98 100644 --- a/index.js +++ b/index.js @@ -5,17 +5,14 @@ import pkgDir from 'pkg-dir'; * @param {string} path */ const resolvePath = (path) => { - let _require = null; + let resolvedPath; try { - _require = require; + resolvedPath = require.resolve(path); } catch { - // Handled + resolvedPath = ( + globalThis.require ?? createRequire(import.meta.url) + ).resolve(path); } - const resolvedPath = ( - _require ?? - globalThis.require ?? - createRequire(import.meta.url) - ).resolve(path); if (!path.includes('./')) { const directory = pkgDir.sync(resolvedPath) ?? ''; return directory;