moduleFile
uses node:path
The file a module path names, as Node and bundlers find it: as it is, with .js/.mjs/.cjs/.json, or its index.js.
import fs from "node:fs"; import path from "node:path"; export function moduleFile(at: string): string | undefined { for (const f of [ at, `${at}.js`, `${at}.mjs`, `${at}.cjs`, `${at}.json`, path.join(at, "index.js"), path.join(at, "index.mjs"), ]) if (fs.existsSync(f) && fs.statSync(f).isFile()) return f; return undefined; }