From a13bd120bc048d77c24126a09ac38b7ac8389ab3 Mon Sep 17 00:00:00 2001 From: Anton Zhukov Date: Wed, 21 Nov 2018 11:56:46 +0300 Subject: [PATCH] Fix find node_modules (#869) --- .../resolve-scripts/src/get_modules_dirs.js | 32 +++++++++++-------- .../src/get_monorepo_node_modules.js | 4 ++- .../core/resolve-scripts/src/resolve_file.js | 2 +- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/packages/core/resolve-scripts/src/get_modules_dirs.js b/packages/core/resolve-scripts/src/get_modules_dirs.js index 35a7d62c7d..7957bb4c35 100644 --- a/packages/core/resolve-scripts/src/get_modules_dirs.js +++ b/packages/core/resolve-scripts/src/get_modules_dirs.js @@ -1,3 +1,4 @@ +import fs from 'fs' import path from 'path' import getMonorepoNodeModules from './get_monorepo_node_modules' @@ -5,21 +6,26 @@ import getMonorepoNodeModules from './get_monorepo_node_modules' const getModulesDirs = ({ isAbsolutePath = false } = {}) => { const currentDir = process.cwd() - const currentDirNodeModules = path.resolve(currentDir, 'node_modules') const monorepoNodeModules = getMonorepoNodeModules() - - const absoluteDirs = [ - currentDirNodeModules, - path.join( - path.dirname( - require.resolve('resolve-runtime/package.json', { - paths: [currentDirNodeModules, ...monorepoNodeModules] - }) - ), - 'node_modules' + const currentDirNodeModules = path.join(currentDir, 'node_modules') + const resolveRuntimeNodeModules = path.join( + path.dirname( + require.resolve('resolve-runtime/package.json', { + paths: [currentDirNodeModules, ...monorepoNodeModules] + }) ), - ...monorepoNodeModules - ] + 'node_modules' + ) + + const absoluteDirs = [] + + if (fs.existsSync(currentDirNodeModules)) { + absoluteDirs.push(currentDirNodeModules) + } + if (fs.existsSync(resolveRuntimeNodeModules)) { + absoluteDirs.push(resolveRuntimeNodeModules) + } + absoluteDirs.push(...monorepoNodeModules) if (isAbsolutePath) { return absoluteDirs diff --git a/packages/core/resolve-scripts/src/get_monorepo_node_modules.js b/packages/core/resolve-scripts/src/get_monorepo_node_modules.js index 6ff9b79678..e5dfdd99b9 100644 --- a/packages/core/resolve-scripts/src/get_monorepo_node_modules.js +++ b/packages/core/resolve-scripts/src/get_monorepo_node_modules.js @@ -48,6 +48,8 @@ const getMonoreposDirs = baseDir => { } const getMonoreposNodeModules = () => - getMonoreposDirs(process.cwd()).map(dir => path.join(dir, 'node_modules')) + getMonoreposDirs(process.cwd()) + .map(dir => path.join(dir, 'node_modules')) + .filter(dir => fs.existsSync(dir)) export default getMonoreposNodeModules diff --git a/packages/core/resolve-scripts/src/resolve_file.js b/packages/core/resolve-scripts/src/resolve_file.js index 71f64bb00a..bd1dcb2bc7 100644 --- a/packages/core/resolve-scripts/src/resolve_file.js +++ b/packages/core/resolve-scripts/src/resolve_file.js @@ -16,7 +16,7 @@ const resolveFile = (query, fallbackQuery) => { require.resolve(query, { paths: [ path.resolve(process.cwd(), 'node_modules'), - path.resolve(__dirname, '../../node_modules'), + path.resolve(__dirname, '../node_modules'), ...getMonorepoNodeModules() ] })