Node Fs


Constants


Synchronous API

AccessSyunc


AppendFileSync


ChModSync


ChOwnSync


CopyFileSync


ExistsSync


LinkSync


MkDirSync


MkDTempSync


ReadDirSync

const fs = require("fs");

function readDirectory(directory) {
  let files = fs.readdirSync(directory);
  return files.sort((a,b) => a.localeCompare(b));
};


ReadFileSync


ReadLinkSync


RealPathSync


RenameSync


RmDirSync


RmSync


StatSync


SymLinkSync


UnlinkSync


UTimesSync


WriteFileSync


Callback API

Access


AppendFile


ChMod


ChOwn


CopyFile


Exists

Deprecated. Use stat or access instead.

Furthermore, do not try using this to check if a file exists before writing it. This will introduce a race condition.

const fs = require("fs");

exists(filename, (err) => {
  if (err) {
    console.log("file exists");
  }
});



MkDir


MkDTemp


ReadDir


ReadFile

const fs = require("fs");

fs.readFile(filename, "utf8", (err, content) => {
  if (err) {
    console.log("cannot read file");
  }
  else {
    console.log(content);
  }
});



RealPath


Rename


RmDir


Rm


Stat




UnwatchFile


UTimes


WatchFile


WriteFile

fs.writeFile(filename, content, (err) => {
  if (err) {
    console.log("cannot write file");
  }
});


See also

Node API for fs


CategoryRicottone

Node/Fs (last edited 2023-02-24 21:00:05 by DominicRicottone)