Skip to main content
Glama
getParentPackageJSON.ts1.44 kB
import { existsSync, readFileSync } from 'node:fs'; import { dirname, resolve } from 'node:path'; type PackageJSON = { name?: string; version?: string; private: boolean; description?: string; homepage?: string; bugs: { url?: string; }; repository: { type?: string; url?: string; }; license?: string; author: { name?: string; url?: string; }; contributors?: { name?: string; email?: string; url?: string; }[]; type?: string; scripts: Record<string, string>; devDependencies: Record<string, string>; packageManager?: string; engines: Record<string, string>; }; export const getParentPackageJSON = (startDir: string): PackageJSON => { let currentDir = startDir; while (currentDir !== dirname(currentDir)) { // Stop when we reach the root const packageJsonPath = resolve(currentDir, 'package.json'); if (existsSync(packageJsonPath)) { return JSON.parse(readFileSync(packageJsonPath, 'utf8')); } // Move up one directory level currentDir = dirname(currentDir); } // Check the root directory as well const rootPackageJsonPath = resolve(currentDir, 'package.json'); if (existsSync(rootPackageJsonPath)) { return JSON.parse(readFileSync(rootPackageJsonPath, 'utf8')); } // If no package.json is found in any parent directory throw new Error( `No package.json found in any parent directory of ${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