Skip to main content
Glama
getPackageJsonPath.ts1.25 kB
import { existsSync } from 'node:fs'; import { dirname, join } from 'node:path'; import { cacheMemory } from './cacheMemory'; export const isESModule = typeof import.meta.url === 'string'; const MAX_LEVELS = 15; type PackageJsonPathCache = { packageJsonPath: string; baseDir: string; }; export const getPackageJsonPath = ( startDir: string = process.cwd() ): PackageJsonPathCache => { const checkedCache = cacheMemory.get<PackageJsonPathCache>( 'packageJsonPath', startDir ); if (checkedCache) return checkedCache; let currentDir = startDir; for (let level = 0; level < MAX_LEVELS; level++) { const packageJsonPath = join(currentDir, 'package.json'); if (existsSync(packageJsonPath)) { cacheMemory.set('packageJsonPath', startDir, { packageJsonPath, baseDir: currentDir, }); return { packageJsonPath, baseDir: currentDir }; } const parentDir = dirname(currentDir); // If we've reached the root directory, stop if (parentDir === currentDir) { break; } currentDir = parentDir; } throw new Error( `Could not find package.json in current directory or any of the ${MAX_LEVELS} parent directories. Searched from: ${startDir}` ); };

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/aymericzip/intlayer'

If you have feedback or need assistance with the MCP directory API, please join our Discord server