Skip to content

feat: handle timers deprecation #241

@AugustinMauroy

Description

@AugustinMauroy

Description

This single codemod addresses multiple deprecations in the node:timers module, migrating from internal, deprecated APIs to public, standard timer functions. It targets the following deprecated methods: timers.enroll(), timers.unenroll(), timers.active(), and timers._unrefActive().

The goal is to replace these internal functions with their modern equivalents, such as setTimeout(), clearTimeout(), and timer.unref(), ensuring code remains functional and free of deprecated APIs.


DEP0095: timers.enroll()

This part of the codemod replaces timers.enroll() with setTimeout() to achieve similar timeout functionality using public APIs.

Before:

const timers = require("node:timers");
const obj = { _idleTimeout: 5000 };
timers.enroll(obj, 5000);

After:

const obj = {};
obj.timer = setTimeout(() => {
  // timeout logic
}, 5000);

DEP0096: timers.unenroll()

This part of the codemod replaces timers.unenroll() with clearTimeout() or clearInterval() for standard timer cancellation.

Before:

const timers = require("node:timers");
const timer = setTimeout(callback, 1000);
timers.unenroll(timer);

After:

const timer = setTimeout(callback, 1000);
clearTimeout(timer);

DEP0126: timers.active()

This part of the codemod removes timers.active() and replaces it with modern timer management, typically using setTimeout().

Before:

const timers = require("node:timers");
timers.active(someTimer);

After:

// Use public timer APIs instead
const timer = setTimeout(() => {
  // timer logic
}, delay);

DEP0127: timers._unrefActive()

This part of the codemod removes timers._unrefActive() and replaces it with a combination of setTimeout() and timer.unref() to maintain the unreferenced behavior.

Before:

const timers = require("node:timers");
timers._unrefActive(someTimer);

After:

// Use public timer APIs instead
const timer = setTimeout(() => {
  // timer logic
}, delay);
timer.unref();

REFS

Metadata

Metadata

Assignees

No one assigned

    Labels

    dep:EoLMigration handles deprecation introduced in an EoL version of nodedep:v24Migration handles deprecation introduced in node v24good first issueGood for newcomershelp wantedExtra attention is neededlet's do it

    Projects

    Status

    🏗 In progress

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions