Skip to content

Commit 9136e10

Browse files
committed
fix(pgai): resolve postgresai/cli export instead of package.json
The previous code tried to require.resolve("postgresai/package.json"), but package.json is not listed in the postgresai package's exports field. Node.js ESM resolution strictly enforces exports, causing the resolution to fail when running via npx/bunx. Now resolves "postgresai/cli" directly, which is properly exported and matches the approach used by the postgres-ai wrapper package.
1 parent 5bff76f commit 9136e10

1 file changed

Lines changed: 5 additions & 9 deletions

File tree

pgai/bin/pgai.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,13 @@ function die(msg: string): never {
1616
}
1717

1818
let target: string;
19+
let isTsFile = false;
1920

2021
// Try to find the postgresai package
2122
try {
22-
// First try to resolve from node_modules - look for the compiled dist version
23-
const postgresaiPkg = require.resolve("postgresai/package.json");
24-
target = resolve(dirname(postgresaiPkg), "dist", "bin", "postgres-ai.js");
25-
26-
// Fallback to source TS if dist doesn't exist (development)
27-
if (!existsSync(target)) {
28-
target = resolve(dirname(postgresaiPkg), "bin", "postgres-ai.ts");
29-
}
23+
// Resolve the exported "cli" entry point from the postgresai package.
24+
// This uses the exports field which is the proper way to resolve ESM packages.
25+
target = require.resolve("postgresai/cli");
3026
} catch {
3127
// Dev-friendly fallback when running from the monorepo checkout (postgresai lives under ../cli).
3228
const fallbackJs = resolve(__dirname, "..", "..", "cli", "dist", "bin", "postgres-ai.js");
@@ -36,6 +32,7 @@ try {
3632
target = fallbackJs;
3733
} else if (existsSync(fallbackTs)) {
3834
target = fallbackTs;
35+
isTsFile = true;
3936
} else {
4037
die(
4138
[
@@ -48,7 +45,6 @@ try {
4845
}
4946

5047
// Determine if we should use node or bun based on the file extension
51-
const isTsFile = target.endsWith(".ts");
5248
const runtime = isTsFile ? "bun" : process.execPath;
5349

5450
const child = spawn(runtime, [target, ...process.argv.slice(2)], {

0 commit comments

Comments
 (0)