From 59b5035851203ba8f3697f44c23affcd66e591a9 Mon Sep 17 00:00:00 2001 From: valadaptive Date: Thu, 22 Feb 2024 00:50:31 -0500 Subject: [PATCH] Seek ENOENTness rather than asking existsSync --- lib/files.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/files.js b/lib/files.js index a14a03ff..b9405f46 100644 --- a/lib/files.js +++ b/lib/files.js @@ -60,11 +60,15 @@ function createSyncImportHook() { function tryReadFileSync(str) { if ( typeof str == 'string' && - str.indexOf(path.sep) !== -1 && - fs.existsSync(str) + str.indexOf(path.sep) !== -1 ) { - // Try interpreting `str` as path to a file. - return fs.readFileSync(str, {encoding: 'utf8'}); + try { + // Try interpreting `str` as path to a file. + return fs.readFileSync(str, {encoding: 'utf8'}); + } catch (err) { + // If the file doesn't exist, return `null`. Rethrow all other errors. + if (err.code !== 'ENOENT') throw err; + } } return null; }