Reading and writing files

Read project files with fs; write temporary files under /tmp.

Runs in Zerethon IDE · Node.js 22 Official JavaScript documentation ↗

A run sees the project's files in its working folder, read-only. A program may write files under /tmp; they are deleted when the run ends.

main.js
const fs = require('fs');

const rows = fs.readFileSync('data.csv', 'utf8').trim().split('\n').slice(1);
const names = rows.map((row) => row.split(',')[0]);
console.log(names);

fs.writeFileSync('/tmp/report.json', JSON.stringify({ count: names.length }));
console.log(JSON.parse(fs.readFileSync('/tmp/report.json', 'utf8')));
data.csv
name,score
ada,90
alan,85

Output

[ 'ada', 'alan' ]
{ count: 2 }
  • Paths are relative to the project root: data/input.txt is the file shown in the explorer under data/.
  • Writing next to your code fails with EROFS: read-only file system — write to /tmp instead.
  • fs.promises.readFile is the async version, for use with await.