`fs.write()` and `fs.writeFileSync()` coercion to string

Aperta
#412 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

@syhstanley ci sta già lavorando.

Dal 29/3/2026.

  • #416 di @syhstanley — aperta

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Tranquilla
Stack tecnologico
javascript, node.js, typescript
Ambito
tooling

Direzione di ricerca

Il payload non nomina alcun file del repository, test o punto di ingresso del codemod; inizia individuando il framework di migrazione esistente e la sua copertura delle API fs di Node.js. Usa i metodi fs elencati, l'esempio node:fs/promises e i casi validi di Buffer e Uint8Array per definire la copertura, quindi verifica le modifiche alla coercizione degli oggetti senza alterare i tipi validi.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

good first issue human help wanted let's do it

Description

Since implicit coercion of objects in fs.write() and fs.writeFileSync() is deprecated (DEP0162) and has reached End-of-Life status in Node.js v19.0.0, we should provide a codemod to replace them.

  • The codemod should convert objects with toString() method to primitive strings before passing to fs write functions.
  • The codemod should handle fs.write(), fs.writeFile(), fs.appendFile(), fs.writeFileSync(), and fs.appendFileSync().
  • The codemod should detect objects being passed as the data parameter.
  • The codemod should add explicit .toString() calls or String() conversion.

Additional Information

Note that in Node.js v19.0.0, implicit coercion of objects with own toString property passed as the second parameter (data) in fs write operations now throws an error. This was deprecated because the implicit conversion could lead to unexpected behavior and made the code less explicit.

Instead of relying on implicit coercion, developers should explicitly convert objects to strings using .toString(), String(), or template literals.

Examples

Example 1: Object in fs.writeFileSync()

Before:

const fs = require("node:fs");

const data = { toString: () => "file content" };
fs.writeFileSync("file.txt", data);

After:

const fs = require("node:fs");

const data = { toString: () => "file content" };
fs.writeFileSync("file.txt", data.toString());
Example 2: Object in fs.writeFile()

Before:

const fs = require("node:fs");

const obj = { toString: () => "content" };
fs.writeFile("file.txt", obj, (err) => {
  if (err) throw err;
});

After:

const fs = require("node:fs");

const obj = { toString: () => "content" };
fs.writeFile("file.txt", obj.toString(), (err) => {
  if (err) throw err;
});
Example 3: Object in fs.appendFileSync()

Before:

const fs = require("node:fs");

const data = { toString: () => "appended data" };
fs.appendFileSync("file.txt", data);

After:

const fs = require("node:fs");

const data = { toString: () => "appended data" };
fs.appendFileSync("file.txt", data.toString());
Example 4: Object in fs.appendFile()

Before:

const fs = require("node:fs");

const content = { toString: () => "more content" };
fs.appendFile("file.txt", content, (err) => {
  if (err) throw err;
});

After:

const fs = require("node:fs");

const content = { toString: () => "more content" };
fs.appendFile("file.txt", content.toString(), (err) => {
  if (err) throw err;
});
Example 5: Object in fs.write()

Before:

const fs = require("node:fs");

const fd = fs.openSync("file.txt", "w");
const data = { toString: () => "buffer content" };
fs.write(fd, data, (err) => {
  if (err) throw err;
  fs.closeSync(fd);
});

After:

const fs = require("node:fs");

const fd = fs.openSync("file.txt", "w");
const data = { toString: () => "buffer content" };
fs.write(fd, data.toString(), (err) => {
  if (err) throw err;
  fs.closeSync(fd);
});
Example 6: Using String() conversion

Before:

const fs = require("node:fs");

const myObj = {
  value: 42,
  toString() {
    return `Value: ${this.value}`;
  }
};
fs.writeFileSync("output.txt", myObj);

After:

const fs = require("node:fs");

const myObj = {
  value: 42,
  toString() {
    return `Value: ${this.value}`;
  }
};
fs.writeFileSync("output.txt", String(myObj));
Example 7: ESM with promises API

Before:

import { writeFile } from "node:fs/promises";

const data = { toString: () => "async content" };
await writeFile("file.txt", data);

After:

import { writeFile } from "node:fs/promises";

const data = { toString: () => "async content" };
await writeFile("file.txt", data.toString());
Example 8: Valid types (no change needed)

Before:

const fs = require("node:fs");

// These are all valid and don't need changes
fs.writeFileSync("file1.txt", "string");
fs.writeFileSync("file2.txt", Buffer.from("buffer"));
fs.writeFileSync("file3.txt", new Uint8Array([1, 2, 3]));

After:

const fs = require("node:fs");

// These are all valid and don't need changes
fs.writeFileSync("file1.txt", "string");
fs.writeFileSync("file2.txt", Buffer.from("buffer"));
fs.writeFileSync("file3.txt", new Uint8Array([1, 2, 3]));

Refs

Lingua principale
TypeScript
Stelle
85
Fork
58
Merge medio
1g 21h
PR unite (30g)
8

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di nodejs/userland-migrations

Tutte le issue di nodejs/userland-migrations

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.