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