fix(pgai): resolve postgresai/cli export instead of package.json
Summary
- Fixed
bunx pgai@dev/npx pgai@devfailing with "failed to locate postgresai package" error - Changed module resolution from
postgresai/package.jsontopostgresai/cli
Problem
When running bunx pgai@dev -V, users encountered:
pgai: failed to locate postgresai package.
This wrapper expects postgresai to be installed as a dependency.
Root cause: The pgai wrapper in pgai/bin/pgai.ts tried to require.resolve("postgresai/package.json"), but the postgresai package's exports field doesn't include ./package.json:
"exports": {
".": "./dist/bin/postgres-ai.js",
"./cli": "./dist/bin/postgres-ai.js"
}
Node.js ESM resolution strictly enforces the exports field, so any subpath not explicitly listed cannot be resolved. This caused the resolution to fail when running via npx/bunx.
Solution
Changed the resolution strategy to resolve postgresai/cli directly instead of postgresai/package.json. The ./cli subpath is properly exported and resolves to the CLI entry point.
This approach aligns with how the postgres-ai wrapper package (cli/packages/postgres-ai/bin/postgres-ai.js) handles the same scenario:
const postgresaiBin = require.resolve('postgresai/cli');
Changes
- pgai/bin/pgai.ts: Replaced require.resolve("postgresai/package.json") with require.resolve("postgresai/cli")
- Simplified the resolution logic by removing the manual path construction to dist/bin/postgres-ai.js
- Fixed isTsFile tracking to only be set in the monorepo fallback path (since postgresai/cli always resolves to .js)
## Test plan
- Run bunx pgai@dev -V and verify it outputs the version
- Run npx pgai@dev -V and verify it outputs the version
- Run from monorepo with bun pgai/bin/pgai.ts -V to verify fallback still works
Create PR
related: #69 (closed)