UNPKG

773 BPlain TextView Raw
1import findUp from 'find-up';
2import path from 'path';
3import {CLIError} from './errors';
4
5/**
6 * Finds project root by looking for a closest `package.json`.
7 */
8export default function findProjectRoot(cwd = process.cwd()): string {
9 const packageLocation = findUp.sync('package.json', {cwd});
10 /**
11 * It is possible that `package.json` doesn't exist
12 * in the tree. In that case, we want to throw an error.
13 *
14 * When executing via `npx`, this will never happen as `npm`
15 * requires that file to be present in order to run.
16 */
17 if (!packageLocation) {
18 throw new CLIError(`
19 We couldn't find a package.json in your project.
20 Are you sure you are running it inside a React Native project?
21 `);
22 }
23
24 return path.dirname(packageLocation);
25}