Skip to content

Instantly share code, notes, and snippets.

@rjmunro
Created December 12, 2025 12:02
Show Gist options
  • Select an option

  • Save rjmunro/04090ed800c0129504e18777a02098fc to your computer and use it in GitHub Desktop.

Select an option

Save rjmunro/04090ed800c0129504e18777a02098fc to your computer and use it in GitHub Desktop.
regex patterns for converting bare Node.js module imports to node: protocol imports
/**
* Script to generate regex patterns for converting bare Node.js module imports to node: protocol imports
*
* This script:
* 1. Takes a list of all Node.js built-in modules (with and without 'node:' prefix)
* 2. Filters to only modules that have both forms (e.g., 'fs' and 'node:fs')
* 3. Generates a search regex to find bare imports: import ... from 'fs'
* 4. Generates a replacement pattern: import ... from 'node:fs'
*
* Output is printed to console for use in find-and-replace operations.
*/
// List taken from https://github.com/sindresorhus/builtin-modules/blob/main/builtin-modules.json
// Includes both legacy bare imports ('fs') and modern protocol imports ('node:fs')
const list = [
"node:assert",
"assert",
"node:assert/strict",
"assert/strict",
"node:async_hooks",
"async_hooks",
"node:buffer",
"buffer",
"node:child_process",
"child_process",
"node:cluster",
"cluster",
"node:console",
"console",
"node:constants",
"constants",
"node:crypto",
"crypto",
"node:dgram",
"dgram",
"node:diagnostics_channel",
"diagnostics_channel",
"node:dns",
"dns",
"node:dns/promises",
"dns/promises",
"node:domain",
"domain",
"node:events",
"events",
"node:fs",
"fs",
"node:fs/promises",
"fs/promises",
"node:http",
"http",
"node:http2",
"http2",
"node:https",
"https",
"node:inspector",
"inspector",
"node:inspector/promises",
"inspector/promises",
"node:module",
"module",
"node:net",
"net",
"node:os",
"os",
"node:path",
"path",
"node:path/posix",
"path/posix",
"node:path/win32",
"path/win32",
"node:perf_hooks",
"perf_hooks",
"node:process",
"process",
"node:querystring",
"querystring",
"node:quic",
"node:readline",
"readline",
"node:readline/promises",
"readline/promises",
"node:repl",
"repl",
"node:sea",
"node:sqlite",
"node:stream",
"stream",
"node:stream/consumers",
"stream/consumers",
"node:stream/promises",
"stream/promises",
"node:stream/web",
"stream/web",
"node:string_decoder",
"string_decoder",
"node:test",
"node:test/reporters",
"node:timers",
"timers",
"node:timers/promises",
"timers/promises",
"node:tls",
"tls",
"node:trace_events",
"trace_events",
"node:tty",
"tty",
"node:url",
"url",
"node:util",
"util",
"node:util/types",
"util/types",
"node:v8",
"v8",
"node:vm",
"vm",
"node:wasi",
"wasi",
"node:worker_threads",
"worker_threads",
"node:zlib",
"zlib"
]
// Filter to find only modules that have both bare and 'node:' prefixed versions
// (e.g., 'fs' is kept because 'node:fs' also exists in the list)
const filtered = list.filter((entry) => list.includes(`node:${entry}`))
// Build regex search pattern to match: import anything from 'module' or 'module/subpath'
// Captures: $1 = import specifiers, $2 = module name, $3 = quote or slash
const search = `import (.*) from '(${filtered.join('|')})('|/)`
// Replacement pattern adds 'node:' prefix to the module name
// Result: import $1 from 'node:$2$3
const replace = `import $1 from 'node:$2$3`
// Output patterns for use in find-and-replace tools (markdown formatted)
console.log(`
## Search Pattern (Regex)
\`\`\`regex
${search}
\`\`\`
## Replace Pattern
\`\`\`
${replace}
\`\`\`
## Sed Example
\`\`\`bash
git ls-files '*.ts' | xargs sed -i '' -E 's#${search}#${replace}#g'
\`\`\`
**Note:** Using \`#\` as delimiter to avoid conflicts with \`/\` in the regex.
This uses \`git ls-files\` to find all TypeScript files in the repository and pipes them to sed via xargs.
`)

Search Pattern (Regex)

import (.*) from '(assert|assert/strict|async_hooks|buffer|child_process|cluster|console|constants|crypto|dgram|diagnostics_channel|dns|dns/promises|domain|events|fs|fs/promises|http|http2|https|inspector|inspector/promises|module|net|os|path|path/posix|path/win32|perf_hooks|process|querystring|readline|readline/promises|repl|stream|stream/consumers|stream/promises|stream/web|string_decoder|timers|timers/promises|tls|trace_events|tty|url|util|util/types|v8|vm|wasi|worker_threads|zlib)('|/)

Replace Pattern

import $1 from 'node:$2$3

Sed Example

git ls-files '*.ts' | xargs sed -i '' -E 's#import (.*) from '(assert|assert/strict|async_hooks|buffer|child_process|cluster|console|constants|crypto|dgram|diagnostics_channel|dns|dns/promises|domain|events|fs|fs/promises|http|http2|https|inspector|inspector/promises|module|net|os|path|path/posix|path/win32|perf_hooks|process|querystring|readline|readline/promises|repl|stream|stream/consumers|stream/promises|stream/web|string_decoder|timers|timers/promises|tls|trace_events|tty|url|util|util/types|v8|vm|wasi|worker_threads|zlib)('|/)#import $1 from 'node:$2$3#g'

Note: Using # as delimiter to avoid conflicts with / in the regex.
This uses git ls-files to find all TypeScript files in the repository and pipes them to sed via xargs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment