MCPcopy Create free account
hub / github.com/commandoperator/cmdop-sdk / moduleDir

Function moduleDir

node/src/locate.ts:50–64  ·  view source on GitHub ↗

* The directory of THIS module, resolved for both ESM (`import.meta.url`) and * CJS (`__dirname`). Anchoring to the module file — never `process.cwd()` — * means the binary is found wherever the package is installed, including * workspaces and symlinked installs.

()

Source from the content-addressed store, hash-verified

48 * workspaces and symlinked installs.
49 */
50function moduleDir(): string {
51 // CJS bundle: tsup emits `__dirname`. ESM bundle: reconstruct from the URL.
52 // Guard `import.meta` access so the CJS build (where it is undefined) does
53 // not throw — tsup rewrites one branch per format, but be defensive.
54 try {
55 // eslint-disable-next-line @typescript-eslint/no-explicit-any
56 const metaUrl = (import.meta as any)?.url as string | undefined;
57 if (metaUrl) return dirname(fileURLToPath(metaUrl));
58 } catch {
59 /* not ESM — fall through to __dirname */
60 }
61 if (typeof __dirname !== "undefined") return __dirname;
62 // Last resort: resolve relative to cwd (should never be reached in practice).
63 return resolve(".");
64}
65
66/**
67 * Find the package root (the dir containing `bin/`). The built bundle lives in

Callers 1

packageBinDirFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected