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 for more details.
Usage
Run this codemod with:
npx codemod nodejs/fs-truncate-fd-deprecation
Before/After
- 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
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.
When working with file descriptors, always use the f-prefixed variants of file system methods: ftruncate(), fchmod(), fstat(), etc.