Skip to content

Bug: AgentDB CLI fails with '__dirname is not defined' in ESM context #77

@adambkovacs

Description

@adambkovacs

Description

The AgentDB CLI (agentdb-cli.js) fails when executed via npx agentdb with the error:

❌ __dirname is not defined

This affects all MCP tools that rely on the CLI:

  • agentdb_stats
  • agentdb_pattern_store
  • agentdb_pattern_search
  • agentdb_pattern_stats

Environment

  • agentic-flow version: 1.10.2
  • Node.js: v22.x
  • OS: macOS (Darwin 25.1.0)

Root Cause

The file dist/agentdb/cli/agentdb-cli.js is an ES module (uses import statements) but references __dirname on line 55:

const schemaPath = path.join(__dirname, '../schemas/frontier-schema.sql');

In ES modules, __dirname and __filename are not available - they only exist in CommonJS modules.

Suggested Fix

Add the standard ESM polyfill at the top of the file after the existing imports:

import * as fs from 'fs';
import * as path from 'path';
import { fileURLToPath } from 'url';

// ESM __dirname polyfill
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

Source File

The fix should be applied to the TypeScript source file (likely src/agentdb/cli/agentdb-cli.ts) so it persists through builds.

Workaround

Users can manually patch the distributed JS file at:

node_modules/agentic-flow/dist/agentdb/cli/agentdb-cli.js

Or for global installs, the path in pnpm's content-addressable store.


Reported by: Adam Kovacs

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions