builtinImport
uses node:module
Where a name Node's builtins export comes from (stripVTControlCharacters from node:util), when exactly one
module exports it (node:fs, not node:fs/promises too: subpaths and deprecated modules aren't asked); undefined
for a name none or several export.
import { builtinModules } from "node:module"; export function builtinImport(name: string): string | undefined { const skip = new Set([ "sys", "punycode", "constants", "_stream_duplex", "_stream_passthrough", "_stream_readable", "_stream_transform", "_stream_writable", "_tls_common", "_tls_wrap", "_http_agent", "_http_client", "_http_common", "_http_incoming", "_http_outgoing", "_http_server", ]); const from = builtinModules.filter((m) => { if (m.includes("/") || skip.has(m) || m.startsWith("_")) return false; try { return Object.prototype.hasOwnProperty.call(process.getBuiltinModule(`node:${m}`), name); } catch { return false; } }); return from.length === 1 ? `import { ${name} } from "node:${from[0]}";` : undefined; }