Skip to main content
This recipe migrates deprecated internals from node:timers to the supported public timers API. It replaces usages of timers.enroll(), timers.unenroll(), timers.active(), and timers._unrefActive() with standard constructs built on top of setTimeout(), clearTimeout(), and Timer#unref().

What It Does

This codemod handles multiple deprecated timer APIs:
  • timers.enroll() - replaced with setTimeout()
  • timers.unenroll() - replaced with clearTimeout()
  • timers.active() - replaced with setTimeout() and unref()
  • timers._unrefActive() - replaced with setTimeout() and unref()

Before/After

Replace timers.enroll()

Before:
const timers = require('node:timers');
const resource = { _idleTimeout: 1500 };
timers.enroll(resource, 1500);
After:
const resource = { timeout: setTimeout(() => {
  // timeout handler
}, 1500) };

Replace timers.unenroll()

Before:
timers.unenroll(resource);
After:
clearTimeout(resource.timeout);

Replace timers.active() and timers._unrefActive()

Before:
const timers = require('node:timers');
timers.active(resource);
timers._unrefActive(resource);
After:
const handle = setTimeout(onTimeout, delay);
handle.unref();

Usage

Run this codemod on your project:
npx codemod node/userland/timers-deprecations
The legacy APIs exposed internal timer bookkeeping fields such as _idleStart or _idleTimeout. Those internals have no public equivalent. The codemod focuses on migrating the control flow to modern timers and leaves application-specific bookkeeping to the developer.
After running this codemod, carefully review the transformed code to ensure that any custom metadata is still updated as expected. You may need to manually adjust timer handling logic.

Build docs developers (and LLMs) love