From 3aebe4fb4ab1b3bbda3c3eb5d25998a03ffe55ec Mon Sep 17 00:00:00 2001 From: William Schurman Date: Thu, 14 Sep 2017 18:19:57 -0700 Subject: [PATCH] [jest-resolve] Prevent default resolver failure when potential resolution directory does not exist (#4483) Summary: A recent change to take precedence of NODE_PATH for module resolution (https://github.com/facebook/jest/pull/4453) made this issue more likely to occur since NODE_PATH could contain non-existent directories. While this could have occurred prior to the change, it was less likely since it is more common that modules are in local `node_modules`. Either way, non-existent directories should be treated the same way as non-existent files during module resolution. The equivalent case for `statSync` returning `ENOENT` for files is `ENOTDIR` for directories. We may want to add `ENOTDIR` to `isFile` as well. Test Plan: 1. `yarn test` 2. Link cli into another project, set NODE_PATH to non-existent directories, no longer see cryptic `Cannot find module` error. Instead, it now resolves to a potential path further down the path list. --- packages/jest-resolve/src/default_resolver.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-resolve/src/default_resolver.js b/packages/jest-resolve/src/default_resolver.js index e72c49370443..b29661f34c38 100644 --- a/packages/jest-resolve/src/default_resolver.js +++ b/packages/jest-resolve/src/default_resolver.js @@ -163,7 +163,7 @@ function isDirectory(dir: Path): boolean { const stat = fs.statSync(dir); result = stat.isDirectory(); } catch (e) { - if (!(e && e.code === 'ENOENT')) { + if (!(e && (e.code === 'ENOENT' || e.code === 'ENOTDIR'))) { throw e; } result = false;