1import findWorkspaceRoot from 'find-yarn-workspace-root'; 2import path from 'path'; 3 4import { env } from './env'; 5 6/** Wraps `findWorkspaceRoot` and guards against having an empty `package.json` file in an upper directory. */ 7export function getWorkspaceRoot(projectRoot: string): string | null { 8 try { 9 return findWorkspaceRoot(projectRoot); 10 } catch (error: any) { 11 if (error.message.includes('Unexpected end of JSON input')) { 12 return null; 13 } 14 throw error; 15 } 16} 17 18export function getModulesPaths(projectRoot: string): string[] { 19 const paths: string[] = []; 20 21 // Only add the project root if it's not the current working directory 22 // this minimizes the chance of Metro resolver breaking on new Node.js versions. 23 const workspaceRoot = getWorkspaceRoot(path.resolve(projectRoot)); // Absolute path or null 24 if (workspaceRoot) { 25 paths.push(path.resolve(projectRoot)); 26 paths.push(path.resolve(workspaceRoot, 'node_modules')); 27 } 28 29 return paths; 30} 31 32export function getServerRoot(projectRoot: string) { 33 return env.EXPO_USE_METRO_WORKSPACE_ROOT 34 ? getWorkspaceRoot(projectRoot) ?? projectRoot 35 : projectRoot; 36} 37