> ## 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.

# fs.truncate() with File Descriptor

> Migrate from fs.truncate() to fs.ftruncate() when using file descriptors (DEP0081)

This recipe transforms the usage of `fs.truncate()` to `fs.ftruncate()` when a file descriptor is used instead of a file path.

## Deprecation

Node.js deprecated using `fs.truncate()` with a file descriptor argument. Use `fs.ftruncate()` instead when working with file descriptors.

See [DEP0081](https://nodejs.org/api/deprecations.html#DEP0081) for more details.

## Usage

Run this codemod with:

```bash theme={null}
npx codemod nodejs/fs-truncate-fd-deprecation
```

## Before/After

```diff theme={null}
- const { truncate, open, close } = require('node:fs');
+ const { ftruncate, open, close } = require('node:fs');

  open('file.txt', 'w', (err, fd) => {
    if (err) throw err;
-   truncate(fd, 10, (err) => {
+   ftruncate(fd, 10, (err) => {
      if (err) throw err;
      close(fd, () => {});
    });
  });
```

## What It Does

* Replaces `truncate` with `ftruncate` when imported from `node:fs` or `fs`
* Updates function calls from `truncate(fd, ...)` to `ftruncate(fd, ...)`
* Applies to callback, synchronous, and promise-based APIs

<Warning>
  This codemod only updates cases where a file descriptor is passed. If you're using `fs.truncate()` with a file path string, no changes will be made.
</Warning>

<Tip>
  When working with file descriptors, always use the `f`-prefixed variants of file system methods: `ftruncate()`, `fchmod()`, `fstat()`, etc.
</Tip>
