> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/nodejs/userland-migrations/llms.txt
> Use this file to discover all available pages before exploring further.

# Node.js Timers Deprecations

> Migrate deprecated timer internals from node:timers to the supported public timers API

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:**

```javascript theme={null}
const timers = require('node:timers');
const resource = { _idleTimeout: 1500 };
timers.enroll(resource, 1500);
```

**After:**

```javascript theme={null}
const resource = { timeout: setTimeout(() => {
  // timeout handler
}, 1500) };
```

### Replace `timers.unenroll()`

**Before:**

```javascript theme={null}
timers.unenroll(resource);
```

**After:**

```javascript theme={null}
clearTimeout(resource.timeout);
```

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

**Before:**

```javascript theme={null}
const timers = require('node:timers');
timers.active(resource);
timers._unrefActive(resource);
```

**After:**

```javascript theme={null}
const handle = setTimeout(onTimeout, delay);
handle.unref();
```

## Usage

Run this codemod on your project:

```bash theme={null}
npx codemod node/userland/timers-deprecations
```

<Warning>
  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.
</Warning>

<Tip>
  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.
</Tip>

## Related Deprecations

* [DEP0095: timers.enroll()](https://nodejs.org/api/deprecations.html#DEP0095)
* [DEP0096: timers.unenroll()](https://nodejs.org/api/deprecations.html#DEP0096)
* [DEP0126: timers.active()](https://nodejs.org/api/deprecations.html#DEP0126)
* [DEP0127: timers.\_unrefActive()](https://nodejs.org/api/deprecations.html#DEP0127)
