{ "source": "doc/api/fs.md", "modules": [ { "textRaw": "File System", "name": "fs", "stability": 2, "stabilityText": "Stable", "desc": "

File I/O is provided by simple wrappers around standard POSIX functions. To\nuse this module do require('fs'). All the methods have asynchronous and\nsynchronous forms.

\n

The asynchronous form always takes a completion callback as its last argument.\nThe arguments passed to the completion callback depend on the method, but the\nfirst argument is always reserved for an exception. If the operation was\ncompleted successfully, then the first argument will be null or undefined.

\n

When using the synchronous form any exceptions are immediately thrown.\nYou can use try/catch to handle exceptions or allow them to bubble up.

\n

Here is an example of the asynchronous version:

\n
const fs = require('fs');\n\nfs.unlink('/tmp/hello', (err) => {\n  if (err) throw err;\n  console.log('successfully deleted /tmp/hello');\n});\n
\n

Here is the synchronous version:

\n
const fs = require('fs');\n\nfs.unlinkSync('/tmp/hello');\nconsole.log('successfully deleted /tmp/hello');\n
\n

With the asynchronous methods there is no guaranteed ordering. So the\nfollowing is prone to error:

\n
fs.rename('/tmp/hello', '/tmp/world', (err) => {\n  if (err) throw err;\n  console.log('renamed complete');\n});\nfs.stat('/tmp/world', (err, stats) => {\n  if (err) throw err;\n  console.log(`stats: ${JSON.stringify(stats)}`);\n});\n
\n

It could be that fs.stat is executed before fs.rename.\nThe correct way to do this is to chain the callbacks.

\n
fs.rename('/tmp/hello', '/tmp/world', (err) => {\n  if (err) throw err;\n  fs.stat('/tmp/world', (err, stats) => {\n    if (err) throw err;\n    console.log(`stats: ${JSON.stringify(stats)}`);\n  });\n});\n
\n

In busy processes, the programmer is strongly encouraged to use the\nasynchronous versions of these calls. The synchronous versions will block\nthe entire process until they complete--halting all connections.

\n

The relative path to a filename can be used. Remember, however, that this path\nwill be relative to process.cwd().

\n

Most fs functions let you omit the callback argument. If you do, a default\ncallback is used that rethrows errors. To get a trace to the original call\nsite, set the NODE_DEBUG environment variable:

\n
$ cat script.js\nfunction bad() {\n  require('fs').readFile('/');\n}\nbad();\n\n$ env NODE_DEBUG=fs node script.js\nfs.js:66\n        throw err;\n              ^\nError: EISDIR, read\n    at rethrow (fs.js:61:21)\n    at maybeCallback (fs.js:79:42)\n    at Object.fs.readFile (fs.js:153:18)\n    at bad (/path/to/script.js:2:17)\n    at Object.<anonymous> (/path/to/script.js:5:1)\n    <etc.>\n
", "classes": [ { "textRaw": "Class: fs.FSWatcher", "type": "class", "name": "fs.FSWatcher", "meta": { "added": [ "v0.5.8" ] }, "desc": "

Objects returned from fs.watch() are of this type.

\n", "events": [ { "textRaw": "Event: 'change'", "type": "event", "name": "change", "meta": { "added": [ "v0.5.8" ] }, "params": [], "desc": "

Emitted when something changes in a watched directory or file.\nSee more details in fs.watch().

\n" }, { "textRaw": "Event: 'error'", "type": "event", "name": "error", "meta": { "added": [ "v0.5.8" ] }, "params": [], "desc": "

Emitted when an error occurs.

\n" } ], "methods": [ { "textRaw": "watcher.close()", "type": "method", "name": "close", "meta": { "added": [ "v0.5.8" ] }, "desc": "

Stop watching for changes on the given fs.FSWatcher.

\n", "signatures": [ { "params": [] } ] } ] }, { "textRaw": "Class: fs.ReadStream", "type": "class", "name": "fs.ReadStream", "meta": { "added": [ "v0.1.93" ] }, "desc": "

ReadStream is a Readable Stream.

\n", "events": [ { "textRaw": "Event: 'open'", "type": "event", "name": "open", "meta": { "added": [ "v0.1.93" ] }, "params": [], "desc": "

Emitted when the ReadStream's file is opened.

\n" }, { "textRaw": "Event: 'close'", "type": "event", "name": "close", "meta": { "added": [ "v0.1.93" ] }, "desc": "

Emitted when the ReadStream's underlying file descriptor has been closed\nusing the fs.close() method.

\n", "params": [] } ], "properties": [ { "textRaw": "readStream.path", "name": "path", "meta": { "added": [ "v0.1.93" ] }, "desc": "

The path to the file the stream is reading from.

\n" } ] }, { "textRaw": "Class: fs.Stats", "type": "class", "name": "fs.Stats", "meta": { "added": [ "v0.1.21" ] }, "desc": "

Objects returned from fs.stat(), fs.lstat() and fs.fstat() and their\nsynchronous counterparts are of this type.

\n\n

For a regular file util.inspect(stats) would return a string very\nsimilar to this:

\n
{\n  dev: 2114,\n  ino: 48064969,\n  mode: 33188,\n  nlink: 1,\n  uid: 85,\n  gid: 100,\n  rdev: 0,\n  size: 527,\n  blksize: 4096,\n  blocks: 8,\n  atime: Mon, 10 Oct 2011 23:24:11 GMT,\n  mtime: Mon, 10 Oct 2011 23:24:11 GMT,\n  ctime: Mon, 10 Oct 2011 23:24:11 GMT,\n  birthtime: Mon, 10 Oct 2011 23:24:11 GMT\n}\n
\n

Please note that atime, mtime, birthtime, and ctime are\ninstances of Date object and to compare the values of\nthese objects you should use appropriate methods. For most general\nuses getTime() will return the number of\nmilliseconds elapsed since 1 January 1970 00:00:00 UTC and this\ninteger should be sufficient for any comparison, however there are\nadditional methods which can be used for displaying fuzzy information.\nMore details can be found in the MDN JavaScript Reference\npage.

\n", "modules": [ { "textRaw": "Stat Time Values", "name": "stat_time_values", "desc": "

The times in the stat object have the following semantics:

\n\n

Prior to Node v0.12, the ctime held the birthtime on Windows\nsystems. Note that as of v0.12, ctime is not "creation time", and\non Unix systems, it never was.

\n", "type": "module", "displayName": "Stat Time Values" } ] }, { "textRaw": "Class: fs.WriteStream", "type": "class", "name": "fs.WriteStream", "meta": { "added": [ "v0.1.93" ] }, "desc": "

WriteStream is a Writable Stream.

\n", "events": [ { "textRaw": "Event: 'open'", "type": "event", "name": "open", "meta": { "added": [ "v0.1.93" ] }, "params": [], "desc": "

Emitted when the WriteStream's file is opened.

\n" }, { "textRaw": "Event: 'close'", "type": "event", "name": "close", "meta": { "added": [ "v0.1.93" ] }, "desc": "

Emitted when the WriteStream's underlying file descriptor has been closed\nusing the fs.close() method.

\n", "params": [] } ], "properties": [ { "textRaw": "writeStream.bytesWritten", "name": "bytesWritten", "meta": { "added": [ "v0.4.7" ] }, "desc": "

The number of bytes written so far. Does not include data that is still queued\nfor writing.

\n" }, { "textRaw": "writeStream.path", "name": "path", "meta": { "added": [ "v0.1.93" ] }, "desc": "

The path to the file the stream is writing to.

\n" } ] } ], "methods": [ { "textRaw": "fs.access(path[, mode], callback)", "type": "method", "name": "access", "meta": { "added": [ "v0.11.15" ] }, "desc": "

Tests a user's permissions for the file specified by path. mode is an\noptional integer that specifies the accessibility checks to be performed. The\nfollowing constants define the possible values of mode. It is possible to\ncreate a mask consisting of the bitwise OR of two or more values.

\n\n

The final argument, callback, is a callback function that is invoked with\na possible error argument. If any of the accessibility checks fail, the error\nargument will be populated. The following example checks if the file\n/etc/passwd can be read and written by the current process.

\n
fs.access('/etc/passwd', fs.R_OK | fs.W_OK, (err) => {\n  console.log(err ? 'no access!' : 'can read/write');\n});\n
\n

Using fs.access() to check for the accessibility of a file before calling\nfs.open(), fs.readFile() or fs.writeFile() is not recommended. Doing\nso introduces a race condition, since other processes may change the file's\nstate between the two calls. Instead, user code should open/read/write the\nfile directly and handle the error raised if the file is not accessible.

\n

For example:

\n

write (NOT RECOMMENDED)

\n
fs.access('myfile', (err) => {\n  if (!err) {\n    console.error('myfile already exists');\n    return;\n  }\n\n  fs.open('myfile', 'wx', (err, fd) => {\n    if (err) throw err;\n    writeMyData(fd);\n  });\n});\n
\n

write (RECOMMENDED)

\n
fs.open('myfile', 'wx', (err, fd) => {\n  if (err) {\n    if (err.code === "EEXIST") {\n      console.error('myfile already exists');\n      return;\n    } else {\n      throw err;\n    }\n  }\n\n  writeMyData(fd);\n});\n
\n

read (NOT RECOMMENDED)

\n
fs.access('myfile', (err) => {\n  if (err) {\n    if (err.code === "ENOENT") {\n      console.error('myfile does not exist');\n      return;\n    } else {\n      throw err;\n    }\n  }\n\n  fs.open('myfile', 'r', (err, fd) => {\n    if (err) throw err;\n    readMyData(fd);\n  });\n});\n
\n

read (RECOMMENDED)

\n
fs.open('myfile', 'r', (err, fd) => {\n  if (err) {\n    if (err.code === "ENOENT") {\n      console.error('myfile does not exist');\n      return;\n    } else {\n      throw err;\n    }\n  }\n\n  readMyData(fd);\n});\n
\n

The "not recommended" examples above check for accessibility and then use the\nfile; the "recommended" examples are better because they use the file directly\nand handle the error, if any.

\n

In general, check for the accessibility of a file only if the file won’t be\nused directly, for example when its accessibility is a signal from another\nprocess.

\n", "signatures": [ { "params": [ { "name": "path" }, { "name": "mode", "optional": true }, { "name": "callback" } ] } ] }, { "textRaw": "fs.accessSync(path[, mode])", "type": "method", "name": "accessSync", "meta": { "added": [ "v0.11.15" ] }, "desc": "

Synchronous version of fs.access(). This throws if any accessibility checks\nfail, and does nothing otherwise.

\n", "signatures": [ { "params": [ { "name": "path" }, { "name": "mode", "optional": true } ] } ] }, { "textRaw": "fs.appendFile(file, data[, options], callback)", "type": "method", "name": "appendFile", "meta": { "added": [ "v0.6.7" ] }, "signatures": [ { "params": [ { "textRaw": "`file` {String} filename ", "name": "file", "type": "String", "desc": "filename" }, { "textRaw": "`data` {String|Buffer} ", "name": "data", "type": "String|Buffer" }, { "textRaw": "`options` {Object|String} ", "options": [ { "textRaw": "`encoding` {String|Null} default = `'utf8'` ", "name": "encoding", "type": "String|Null", "desc": "default = `'utf8'`" }, { "textRaw": "`mode` {Number} default = `0o666` ", "name": "mode", "type": "Number", "desc": "default = `0o666`" }, { "textRaw": "`flag` {String} default = `'a'` ", "name": "flag", "type": "String", "desc": "default = `'a'`" } ], "name": "options", "type": "Object|String", "optional": true }, { "textRaw": "`callback` {Function} ", "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "file" }, { "name": "data" }, { "name": "options", "optional": true }, { "name": "callback" } ] } ], "desc": "

Asynchronously append data to a file, creating the file if it does not yet exist.\ndata can be a string or a buffer.

\n

Example:

\n
fs.appendFile('message.txt', 'data to append', (err) => {\n  if (err) throw err;\n  console.log('The "data to append" was appended to file!');\n});\n
\n

If options is a string, then it specifies the encoding. Example:

\n
fs.appendFile('message.txt', 'data to append', 'utf8', callback);\n
\n" }, { "textRaw": "fs.appendFileSync(file, data[, options])", "type": "method", "name": "appendFileSync", "meta": { "added": [ "v0.6.7" ] }, "desc": "

The synchronous version of fs.appendFile(). Returns undefined.

\n", "signatures": [ { "params": [ { "name": "file" }, { "name": "data" }, { "name": "options", "optional": true } ] } ] }, { "textRaw": "fs.chmod(path, mode, callback)", "type": "method", "name": "chmod", "meta": { "added": [ "v0.1.30" ] }, "desc": "

Asynchronous chmod(2). No arguments other than a possible exception are given\nto the completion callback.

\n", "signatures": [ { "params": [ { "name": "path" }, { "name": "mode" }, { "name": "callback" } ] } ] }, { "textRaw": "fs.chmodSync(path, mode)", "type": "method", "name": "chmodSync", "meta": { "added": [ "v0.6.7" ] }, "desc": "

Synchronous chmod(2). Returns undefined.

\n", "signatures": [ { "params": [ { "name": "path" }, { "name": "mode" } ] } ] }, { "textRaw": "fs.chown(path, uid, gid, callback)", "type": "method", "name": "chown", "meta": { "added": [ "v0.1.97" ] }, "desc": "

Asynchronous chown(2). No arguments other than a possible exception are given\nto the completion callback.

\n", "signatures": [ { "params": [ { "name": "path" }, { "name": "uid" }, { "name": "gid" }, { "name": "callback" } ] } ] }, { "textRaw": "fs.chownSync(path, uid, gid)", "type": "method", "name": "chownSync", "meta": { "added": [ "v0.1.97" ] }, "desc": "

Synchronous chown(2). Returns undefined.

\n", "signatures": [ { "params": [ { "name": "path" }, { "name": "uid" }, { "name": "gid" } ] } ] }, { "textRaw": "fs.close(fd, callback)", "type": "method", "name": "close", "meta": { "added": [ "v0.0.2" ] }, "desc": "

Asynchronous close(2). No arguments other than a possible exception are given\nto the completion callback.

\n", "signatures": [ { "params": [ { "name": "fd" }, { "name": "callback" } ] } ] }, { "textRaw": "fs.closeSync(fd)", "type": "method", "name": "closeSync", "meta": { "added": [ "v0.1.21" ] }, "desc": "

Synchronous close(2). Returns undefined.

\n", "signatures": [ { "params": [ { "name": "fd" } ] } ] }, { "textRaw": "fs.createReadStream(path[, options])", "type": "method", "name": "createReadStream", "meta": { "added": [ "v0.1.31" ] }, "desc": "

Returns a new ReadStream object. (See Readable Stream).

\n

Be aware that, unlike the default value set for highWaterMark on a\nreadable stream (16 kb), the stream returned by this method has a\ndefault value of 64 kb for the same parameter.

\n

options is an object or string with the following defaults:

\n
{\n  flags: 'r',\n  encoding: null,\n  fd: null,\n  mode: 0o666,\n  autoClose: true\n}\n
\n

options can include start and end values to read a range of bytes from\nthe file instead of the entire file. Both start and end are inclusive and\nstart counting at 0. If fd is specified and start is omitted or undefined,\nfs.createReadStream() reads sequentially from the current file position.\nThe encoding can be any one of those accepted by Buffer.

\n

If fd is specified, ReadStream will ignore the path argument and will use\nthe specified file descriptor. This means that no 'open' event will be emitted.\nNote that fd should be blocking; non-blocking fds should be passed to\nnet.Socket.

\n

If autoClose is false, then the file descriptor won't be closed, even if\nthere's an error. It is your responsibility to close it and make sure\nthere's no file descriptor leak. If autoClose is set to true (default\nbehavior), on error or end the file descriptor will be closed\nautomatically.

\n

mode sets the file mode (permission and sticky bits), but only if the\nfile was created.

\n

An example to read the last 10 bytes of a file which is 100 bytes long:

\n
fs.createReadStream('sample.txt', {start: 90, end: 99});\n
\n

If options is a string, then it specifies the encoding.

\n", "signatures": [ { "params": [ { "name": "path" }, { "name": "options", "optional": true } ] } ] }, { "textRaw": "fs.createWriteStream(path[, options])", "type": "method", "name": "createWriteStream", "meta": { "added": [ "v0.1.31" ] }, "desc": "

Returns a new WriteStream object. (See Writable Stream).

\n

options is an object or string with the following defaults:

\n
{\n  flags: 'w',\n  defaultEncoding: 'utf8',\n  fd: null,\n  mode: 0o666\n}\n
\n

options may also include a start option to allow writing data at\nsome position past the beginning of the file. Modifying a file rather\nthan replacing it may require a flags mode of r+ rather than the\ndefault mode w. The defaultEncoding can be any one of those accepted by Buffer.

\n

Like ReadStream, if fd is specified, WriteStream will ignore the\npath argument and will use the specified file descriptor. This means that no\n'open' event will be emitted. Note that fd should be blocking; non-blocking\nfds should be passed to net.Socket.

\n

If options is a string, then it specifies the encoding.

\n", "signatures": [ { "params": [ { "name": "path" }, { "name": "options", "optional": true } ] } ] }, { "textRaw": "fs.exists(path, callback)", "type": "method", "name": "exists", "meta": { "added": [ "v0.0.2" ], "deprecated": [ "v1.0.0" ] }, "stability": 0, "stabilityText": "Deprecated: Use [`fs.stat()`][] or [`fs.access()`][] instead.", "desc": "

Test whether or not the given path exists by checking with the file system.\nThen call the callback argument with either true or false. Example:

\n
fs.exists('/etc/passwd', (exists) => {\n  console.log(exists ? 'it\\'s there' : 'no passwd!');\n});\n
\n

Using fs.exists() to check for the existence of a file before calling\nfs.open(), fs.readFile() or fs.writeFile() is not recommended. Doing\nso introduces a race condition, since other processes may change the file's\nstate between the two calls. Instead, user code should open/read/write the\nfile directly and handle the error raised if the file does not exist.

\n

For example:

\n

write (NOT RECOMMENDED)

\n
fs.exists('myfile', (exists) => {\n  if (exists) {\n    console.error('myfile already exists');\n  } else {\n    fs.open('myfile', 'wx', (err, fd) => {\n      if (err) throw err;\n      writeMyData(fd);\n    });\n  }\n});\n
\n

write (RECOMMENDED)

\n
fs.open('myfile', 'wx', (err, fd) => {\n  if (err) {\n    if (err.code === "EEXIST") {\n      console.error('myfile already exists');\n      return;\n    } else {\n      throw err;\n    }\n  }\n  writeMyData(fd);\n});\n
\n

read (NOT RECOMMENDED)

\n
fs.exists('myfile', (exists) => {\n  if (exists) {\n    fs.open('myfile', 'r', (err, fd) => {\n      readMyData(fd);\n    });\n  } else {\n    console.error('myfile does not exist');\n  }\n});\n
\n

read (RECOMMENDED)

\n
fs.open('myfile', 'r', (err, fd) => {\n  if (err) {\n    if (err.code === "ENOENT") {\n      console.error('myfile does not exist');\n      return;\n    } else {\n      throw err;\n    }\n  } else {\n    readMyData(fd);\n  }\n});\n
\n

The "not recommended" examples above check for existence and then use the\nfile; the "recommended" examples are better because they use the file directly\nand handle the error, if any.

\n

In general, check for the existence of a file only if the file won’t be\nused directly, for example when its existence is a signal from another\nprocess.

\n", "signatures": [ { "params": [ { "name": "path" }, { "name": "callback" } ] } ] }, { "textRaw": "fs.existsSync(path)", "type": "method", "name": "existsSync", "meta": { "added": [ "v0.1.21" ], "deprecated": [ "v1.0.0" ] }, "desc": "

Stability: 0 - Deprecated: Use fs.statSync() or fs.accessSync()\ninstead.

\n

Synchronous version of fs.exists().\nReturns true if the file exists, false otherwise.

\n", "signatures": [ { "params": [ { "name": "path" } ] } ] }, { "textRaw": "fs.fchmod(fd, mode, callback)", "type": "method", "name": "fchmod", "meta": { "added": [ "v0.4.7" ] }, "desc": "

Asynchronous fchmod(2). No arguments other than a possible exception\nare given to the completion callback.

\n", "signatures": [ { "params": [ { "name": "fd" }, { "name": "mode" }, { "name": "callback" } ] } ] }, { "textRaw": "fs.fchmodSync(fd, mode)", "type": "method", "name": "fchmodSync", "meta": { "added": [ "v0.4.7" ] }, "desc": "

Synchronous fchmod(2). Returns undefined.

\n", "signatures": [ { "params": [ { "name": "fd" }, { "name": "mode" } ] } ] }, { "textRaw": "fs.fchown(fd, uid, gid, callback)", "type": "method", "name": "fchown", "meta": { "added": [ "v0.4.7" ] }, "desc": "

Asynchronous fchown(2). No arguments other than a possible exception are given\nto the completion callback.

\n", "signatures": [ { "params": [ { "name": "fd" }, { "name": "uid" }, { "name": "gid" }, { "name": "callback" } ] } ] }, { "textRaw": "fs.fchownSync(fd, uid, gid)", "type": "method", "name": "fchownSync", "meta": { "added": [ "v0.4.7" ] }, "desc": "

Synchronous fchown(2). Returns undefined.

\n", "signatures": [ { "params": [ { "name": "fd" }, { "name": "uid" }, { "name": "gid" } ] } ] }, { "textRaw": "fs.fdatasync(fd, callback)", "type": "method", "name": "fdatasync", "meta": { "added": [ "v0.1.96" ] }, "desc": "

Asynchronous fdatasync(2). No arguments other than a possible exception are\ngiven to the completion callback.

\n", "signatures": [ { "params": [ { "name": "fd" }, { "name": "callback" } ] } ] }, { "textRaw": "fs.fdatasyncSync(fd)", "type": "method", "name": "fdatasyncSync", "meta": { "added": [ "v0.1.96" ] }, "desc": "

Synchronous fdatasync(2). Returns undefined.

\n", "signatures": [ { "params": [ { "name": "fd" } ] } ] }, { "textRaw": "fs.fstat(fd, callback)", "type": "method", "name": "fstat", "meta": { "added": [ "v0.1.95" ] }, "desc": "

Asynchronous fstat(2). The callback gets two arguments (err, stats) where\nstats is a fs.Stats object. fstat() is identical to stat(),\nexcept that the file to be stat-ed is specified by the file descriptor fd.

\n", "signatures": [ { "params": [ { "name": "fd" }, { "name": "callback" } ] } ] }, { "textRaw": "fs.fstatSync(fd)", "type": "method", "name": "fstatSync", "meta": { "added": [ "v0.1.95" ] }, "desc": "

Synchronous fstat(2). Returns an instance of fs.Stats.

\n", "signatures": [ { "params": [ { "name": "fd" } ] } ] }, { "textRaw": "fs.fsync(fd, callback)", "type": "method", "name": "fsync", "meta": { "added": [ "v0.1.96" ] }, "desc": "

Asynchronous fsync(2). No arguments other than a possible exception are given\nto the completion callback.

\n", "signatures": [ { "params": [ { "name": "fd" }, { "name": "callback" } ] } ] }, { "textRaw": "fs.fsyncSync(fd)", "type": "method", "name": "fsyncSync", "meta": { "added": [ "v0.1.96" ] }, "desc": "

Synchronous fsync(2). Returns undefined.

\n", "signatures": [ { "params": [ { "name": "fd" } ] } ] }, { "textRaw": "fs.ftruncate(fd, len, callback)", "type": "method", "name": "ftruncate", "meta": { "added": [ "v0.8.6" ] }, "desc": "

Asynchronous ftruncate(2). No arguments other than a possible exception are\ngiven to the completion callback.

\n", "signatures": [ { "params": [ { "name": "fd" }, { "name": "len" }, { "name": "callback" } ] } ] }, { "textRaw": "fs.ftruncateSync(fd, len)", "type": "method", "name": "ftruncateSync", "meta": { "added": [ "v0.8.6" ] }, "desc": "

Synchronous ftruncate(2). Returns undefined.

\n", "signatures": [ { "params": [ { "name": "fd" }, { "name": "len" } ] } ] }, { "textRaw": "fs.futimes(fd, atime, mtime, callback)", "type": "method", "name": "futimes", "meta": { "added": [ "v0.4.2" ] }, "desc": "

Change the file timestamps of a file referenced by the supplied file\ndescriptor.

\n", "signatures": [ { "params": [ { "name": "fd" }, { "name": "atime" }, { "name": "mtime" }, { "name": "callback" } ] } ] }, { "textRaw": "fs.futimesSync(fd, atime, mtime)", "type": "method", "name": "futimesSync", "meta": { "added": [ "v0.4.2" ] }, "desc": "

Synchronous version of fs.futimes(). Returns undefined.

\n", "signatures": [ { "params": [ { "name": "fd" }, { "name": "atime" }, { "name": "mtime" } ] } ] }, { "textRaw": "fs.lchmod(path, mode, callback)", "type": "method", "name": "lchmod", "meta": { "deprecated": [ "v0.4.7" ] }, "desc": "

Asynchronous lchmod(2). No arguments other than a possible exception\nare given to the completion callback.

\n

Only available on Mac OS X.

\n", "signatures": [ { "params": [ { "name": "path" }, { "name": "mode" }, { "name": "callback" } ] } ] }, { "textRaw": "fs.lchmodSync(path, mode)", "type": "method", "name": "lchmodSync", "meta": { "deprecated": [ "v0.4.7" ] }, "desc": "

Synchronous lchmod(2). Returns undefined.

\n", "signatures": [ { "params": [ { "name": "path" }, { "name": "mode" } ] } ] }, { "textRaw": "fs.lchown(path, uid, gid, callback)", "type": "method", "name": "lchown", "meta": { "deprecated": [ "v0.4.7" ] }, "desc": "

Asynchronous lchown(2). No arguments other than a possible exception are given\nto the completion callback.

\n", "signatures": [ { "params": [ { "name": "path" }, { "name": "uid" }, { "name": "gid" }, { "name": "callback" } ] } ] }, { "textRaw": "fs.lchownSync(path, uid, gid)", "type": "method", "name": "lchownSync", "meta": { "deprecated": [ "v0.4.7" ] }, "desc": "

Synchronous lchown(2). Returns undefined.

\n", "signatures": [ { "params": [ { "name": "path" }, { "name": "uid" }, { "name": "gid" } ] } ] }, { "textRaw": "fs.link(srcpath, dstpath, callback)", "type": "method", "name": "link", "meta": { "added": [ "v0.1.31" ] }, "desc": "

Asynchronous link(2). No arguments other than a possible exception are given to\nthe completion callback.

\n", "signatures": [ { "params": [ { "name": "srcpath" }, { "name": "dstpath" }, { "name": "callback" } ] } ] }, { "textRaw": "fs.linkSync(srcpath, dstpath)", "type": "method", "name": "linkSync", "meta": { "added": [ "v0.1.31" ] }, "desc": "

Synchronous link(2). Returns undefined.

\n", "signatures": [ { "params": [ { "name": "srcpath" }, { "name": "dstpath" } ] } ] }, { "textRaw": "fs.lstat(path, callback)", "type": "method", "name": "lstat", "meta": { "added": [ "v0.1.30" ] }, "desc": "

Asynchronous lstat(2). The callback gets two arguments (err, stats) where\nstats is a fs.Stats object. lstat() is identical to stat(),\nexcept that if path is a symbolic link, then the link itself is stat-ed,\nnot the file that it refers to.

\n", "signatures": [ { "params": [ { "name": "path" }, { "name": "callback" } ] } ] }, { "textRaw": "fs.lstatSync(path)", "type": "method", "name": "lstatSync", "meta": { "added": [ "v0.1.30" ] }, "desc": "

Synchronous lstat(2). Returns an instance of fs.Stats.

\n", "signatures": [ { "params": [ { "name": "path" } ] } ] }, { "textRaw": "fs.mkdir(path[, mode], callback)", "type": "method", "name": "mkdir", "meta": { "added": [ "v0.1.8" ] }, "desc": "

Asynchronous mkdir(2). No arguments other than a possible exception are given\nto the completion callback. mode defaults to 0o777.

\n", "signatures": [ { "params": [ { "name": "path" }, { "name": "mode", "optional": true }, { "name": "callback" } ] } ] }, { "textRaw": "fs.mkdirSync(path[, mode])", "type": "method", "name": "mkdirSync", "meta": { "added": [ "v0.1.21" ] }, "desc": "

Synchronous mkdir(2). Returns undefined.

\n", "signatures": [ { "params": [ { "name": "path" }, { "name": "mode", "optional": true } ] } ] }, { "textRaw": "fs.mkdtemp(prefix, callback)", "type": "method", "name": "mkdtemp", "desc": "

Creates a unique temporary directory.

\n

Generates six random characters to be appended behind a required\nprefix to create a unique temporary directory.

\n

The created folder path is passed as a string to the callback's second\nparameter.

\n

Example:

\n
fs.mkdtemp('/tmp/foo-', (err, folder) => {\n  console.log(folder);\n    // Prints: /tmp/foo-itXde2\n});\n
\n", "signatures": [ { "params": [ { "name": "prefix" }, { "name": "callback" } ] } ] }, { "textRaw": "fs.mkdtempSync(template)", "type": "method", "name": "mkdtempSync", "desc": "

The synchronous version of [fs.mkdtemp()][]. Returns the created\nfolder path.

\n", "signatures": [ { "params": [ { "name": "template" } ] } ] }, { "textRaw": "fs.open(path, flags[, mode], callback)", "type": "method", "name": "open", "meta": { "added": [ "v0.0.2" ] }, "desc": "

Asynchronous file open. See open(2). flags can be:

\n\n

mode sets the file mode (permission and sticky bits), but only if the file was\ncreated. It defaults to 0666, readable and writable.

\n

The callback gets two arguments (err, fd).

\n

The exclusive flag 'x' (O_EXCL flag in open(2)) ensures that path is newly\ncreated. On POSIX systems, path is considered to exist even if it is a symlink\nto a non-existent file. The exclusive flag may or may not work with network file\nsystems.

\n

flags can also be a number as documented by open(2); commonly used constants\nare available from require('constants'). On Windows, flags are translated to\ntheir equivalent ones where applicable, e.g. O_WRONLY to FILE_GENERIC_WRITE,\nor O_EXCL|O_CREAT to CREATE_NEW, as accepted by CreateFileW.

\n

On Linux, positional writes don't work when the file is opened in append mode.\nThe kernel ignores the position argument and always appends the data to\nthe end of the file.

\n", "signatures": [ { "params": [ { "name": "path" }, { "name": "flags" }, { "name": "mode", "optional": true }, { "name": "callback" } ] } ] }, { "textRaw": "fs.openSync(path, flags[, mode])", "type": "method", "name": "openSync", "meta": { "added": [ "v0.1.21" ] }, "desc": "

Synchronous version of fs.open(). Returns an integer representing the file\ndescriptor.

\n", "signatures": [ { "params": [ { "name": "path" }, { "name": "flags" }, { "name": "mode", "optional": true } ] } ] }, { "textRaw": "fs.read(fd, buffer, offset, length, position, callback)", "type": "method", "name": "read", "meta": { "added": [ "v0.0.2" ] }, "desc": "

Read data from the file specified by fd.

\n

buffer is the buffer that the data will be written to.

\n

offset is the offset in the buffer to start writing at.

\n

length is an integer specifying the number of bytes to read.

\n

position is an integer specifying where to begin reading from in the file.\nIf position is null, data will be read from the current file position.

\n

The callback is given the three arguments, (err, bytesRead, buffer).

\n", "signatures": [ { "params": [ { "name": "fd" }, { "name": "buffer" }, { "name": "offset" }, { "name": "length" }, { "name": "position" }, { "name": "callback" } ] } ] }, { "textRaw": "fs.readdir(path, callback)", "type": "method", "name": "readdir", "meta": { "added": [ "v0.1.8" ] }, "desc": "

Asynchronous readdir(3). Reads the contents of a directory.\nThe callback gets two arguments (err, files) where files is an array of\nthe names of the files in the directory excluding '.' and '..'.

\n", "signatures": [ { "params": [ { "name": "path" }, { "name": "callback" } ] } ] }, { "textRaw": "fs.readdirSync(path)", "type": "method", "name": "readdirSync", "meta": { "added": [ "v0.1.21" ] }, "desc": "

Synchronous readdir(3). Returns an array of filenames excluding '.' and\n'..'.

\n", "signatures": [ { "params": [ { "name": "path" } ] } ] }, { "textRaw": "fs.readFile(file[, options], callback)", "type": "method", "name": "readFile", "meta": { "added": [ "v0.1.29" ] }, "signatures": [ { "params": [ { "textRaw": "`file` {String} filename ", "name": "file", "type": "String", "desc": "filename" }, { "textRaw": "`options` {Object | String} ", "options": [ { "textRaw": "`encoding` {String | Null} default = `null` ", "name": "encoding", "type": "String | Null", "desc": "default = `null`" }, { "textRaw": "`flag` {String} default = `'r'` ", "name": "flag", "type": "String", "desc": "default = `'r'`" } ], "name": "options", "type": "Object | String", "optional": true }, { "textRaw": "`callback` {Function} ", "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "file" }, { "name": "options", "optional": true }, { "name": "callback" } ] } ], "desc": "

Asynchronously reads the entire contents of a file. Example:

\n
fs.readFile('/etc/passwd', (err, data) => {\n  if (err) throw err;\n  console.log(data);\n});\n
\n

The callback is passed two arguments (err, data), where data is the\ncontents of the file.

\n

If no encoding is specified, then the raw buffer is returned.

\n

If options is a string, then it specifies the encoding. Example:

\n
fs.readFile('/etc/passwd', 'utf8', callback);\n
\n" }, { "textRaw": "fs.readFileSync(file[, options])", "type": "method", "name": "readFileSync", "meta": { "added": [ "v0.1.8" ] }, "desc": "

Synchronous version of fs.readFile. Returns the contents of the file.

\n

If the encoding option is specified then this function returns a\nstring. Otherwise it returns a buffer.

\n", "signatures": [ { "params": [ { "name": "file" }, { "name": "options", "optional": true } ] } ] }, { "textRaw": "fs.readlink(path, callback)", "type": "method", "name": "readlink", "meta": { "added": [ "v0.1.31" ] }, "desc": "

Asynchronous readlink(2). The callback gets two arguments (err,\nlinkString).

\n", "signatures": [ { "params": [ { "name": "path" }, { "name": "callback" } ] } ] }, { "textRaw": "fs.readlinkSync(path)", "type": "method", "name": "readlinkSync", "meta": { "added": [ "v0.1.31" ] }, "desc": "

Synchronous readlink(2). Returns the symbolic link's string value.

\n", "signatures": [ { "params": [ { "name": "path" } ] } ] }, { "textRaw": "fs.realpath(path[, cache], callback)", "type": "method", "name": "realpath", "meta": { "added": [ "v0.1.31" ] }, "desc": "

Asynchronous realpath(2). The callback gets two arguments (err,\nresolvedPath). May use process.cwd to resolve relative paths. cache is an\nobject literal of mapped paths that can be used to force a specific path\nresolution or avoid additional fs.stat calls for known real paths.

\n

Example:

\n
var cache = {'/etc':'/private/etc'};\nfs.realpath('/etc/passwd', cache, (err, resolvedPath) => {\n  if (err) throw err;\n  console.log(resolvedPath);\n});\n
\n", "signatures": [ { "params": [ { "name": "path" }, { "name": "cache", "optional": true }, { "name": "callback" } ] } ] }, { "textRaw": "fs.readSync(fd, buffer, offset, length, position)", "type": "method", "name": "readSync", "meta": { "added": [ "v0.1.21" ] }, "desc": "

Synchronous version of fs.read(). Returns the number of bytesRead.

\n", "signatures": [ { "params": [ { "name": "fd" }, { "name": "buffer" }, { "name": "offset" }, { "name": "length" }, { "name": "position" } ] } ] }, { "textRaw": "fs.realpathSync(path[, cache])", "type": "method", "name": "realpathSync", "meta": { "added": [ "v0.1.31" ] }, "desc": "

Synchronous realpath(2). Returns the resolved path. cache is an\nobject literal of mapped paths that can be used to force a specific path\nresolution or avoid additional fs.stat calls for known real paths.

\n", "signatures": [ { "params": [ { "name": "path" }, { "name": "cache", "optional": true } ] } ] }, { "textRaw": "fs.rename(oldPath, newPath, callback)", "type": "method", "name": "rename", "meta": { "added": [ "v0.0.2" ] }, "desc": "

Asynchronous rename(2). No arguments other than a possible exception are given\nto the completion callback.

\n", "signatures": [ { "params": [ { "name": "oldPath" }, { "name": "newPath" }, { "name": "callback" } ] } ] }, { "textRaw": "fs.renameSync(oldPath, newPath)", "type": "method", "name": "renameSync", "meta": { "added": [ "v0.1.21" ] }, "desc": "

Synchronous rename(2). Returns undefined.

\n", "signatures": [ { "params": [ { "name": "oldPath" }, { "name": "newPath" } ] } ] }, { "textRaw": "fs.rmdir(path, callback)", "type": "method", "name": "rmdir", "meta": { "added": [ "v0.0.2" ] }, "desc": "

Asynchronous rmdir(2). No arguments other than a possible exception are given\nto the completion callback.

\n", "signatures": [ { "params": [ { "name": "path" }, { "name": "callback" } ] } ] }, { "textRaw": "fs.rmdirSync(path)", "type": "method", "name": "rmdirSync", "meta": { "added": [ "v0.1.21" ] }, "desc": "

Synchronous rmdir(2). Returns undefined.

\n", "signatures": [ { "params": [ { "name": "path" } ] } ] }, { "textRaw": "fs.stat(path, callback)", "type": "method", "name": "stat", "meta": { "added": [ "v0.0.2" ] }, "desc": "

Asynchronous stat(2). The callback gets two arguments (err, stats) where\nstats is a fs.Stats object. See the fs.Stats section for more\ninformation.

\n", "signatures": [ { "params": [ { "name": "path" }, { "name": "callback" } ] } ] }, { "textRaw": "fs.statSync(path)", "type": "method", "name": "statSync", "meta": { "added": [ "v0.1.21" ] }, "desc": "

Synchronous stat(2). Returns an instance of fs.Stats.

\n", "signatures": [ { "params": [ { "name": "path" } ] } ] }, { "textRaw": "fs.symlink(target, path[, type], callback)", "type": "method", "name": "symlink", "meta": { "added": [ "v0.1.31" ] }, "desc": "

Asynchronous symlink(2). No arguments other than a possible exception are given\nto the completion callback.\nThe type argument can be set to 'dir', 'file', or 'junction' (default\nis 'file') and is only available on Windows (ignored on other platforms).\nNote that Windows junction points require the destination path to be absolute. When using\n'junction', the target argument will automatically be normalized to absolute path.

\n

Here is an example below:

\n
fs.symlink('./foo', './new-port');\n
\n

It creates a symbolic link named "new-port" that points to "foo".

\n", "signatures": [ { "params": [ { "name": "target" }, { "name": "path" }, { "name": "type", "optional": true }, { "name": "callback" } ] } ] }, { "textRaw": "fs.symlinkSync(target, path[, type])", "type": "method", "name": "symlinkSync", "meta": { "added": [ "v0.1.31" ] }, "desc": "

Synchronous symlink(2). Returns undefined.

\n", "signatures": [ { "params": [ { "name": "target" }, { "name": "path" }, { "name": "type", "optional": true } ] } ] }, { "textRaw": "fs.truncate(path, len, callback)", "type": "method", "name": "truncate", "meta": { "added": [ "v0.8.6" ] }, "desc": "

Asynchronous truncate(2). No arguments other than a possible exception are\ngiven to the completion callback. A file descriptor can also be passed as the\nfirst argument. In this case, fs.ftruncate() is called.

\n", "signatures": [ { "params": [ { "name": "path" }, { "name": "len" }, { "name": "callback" } ] } ] }, { "textRaw": "fs.truncateSync(path, len)", "type": "method", "name": "truncateSync", "meta": { "added": [ "v0.8.6" ] }, "desc": "

Synchronous truncate(2). Returns undefined.

\n", "signatures": [ { "params": [ { "name": "path" }, { "name": "len" } ] } ] }, { "textRaw": "fs.unlink(path, callback)", "type": "method", "name": "unlink", "meta": { "added": [ "v0.0.2" ] }, "desc": "

Asynchronous unlink(2). No arguments other than a possible exception are given\nto the completion callback.

\n", "signatures": [ { "params": [ { "name": "path" }, { "name": "callback" } ] } ] }, { "textRaw": "fs.unlinkSync(path)", "type": "method", "name": "unlinkSync", "meta": { "added": [ "v0.1.21" ] }, "desc": "

Synchronous unlink(2). Returns undefined.

\n", "signatures": [ { "params": [ { "name": "path" } ] } ] }, { "textRaw": "fs.unwatchFile(filename[, listener])", "type": "method", "name": "unwatchFile", "meta": { "added": [ "v0.1.31" ] }, "desc": "

Stop watching for changes on filename. If listener is specified, only that\nparticular listener is removed. Otherwise, all listeners are removed and you\nhave effectively stopped watching filename.

\n

Calling fs.unwatchFile() with a filename that is not being watched is a\nno-op, not an error.

\n

Note: fs.watch() is more efficient than fs.watchFile() and fs.unwatchFile().\nfs.watch() should be used instead of fs.watchFile() and fs.unwatchFile()\nwhen possible.

\n", "signatures": [ { "params": [ { "name": "filename" }, { "name": "listener", "optional": true } ] } ] }, { "textRaw": "fs.utimes(path, atime, mtime, callback)", "type": "method", "name": "utimes", "meta": { "added": [ "v0.4.2" ] }, "desc": "

Change file timestamps of the file referenced by the supplied path.

\n

Note: the arguments atime and mtime of the following related functions does\nfollow the below rules:

\n\n", "signatures": [ { "params": [ { "name": "path" }, { "name": "atime" }, { "name": "mtime" }, { "name": "callback" } ] } ] }, { "textRaw": "fs.utimesSync(path, atime, mtime)", "type": "method", "name": "utimesSync", "meta": { "added": [ "v0.4.2" ] }, "desc": "

Synchronous version of fs.utimes(). Returns undefined.

\n", "signatures": [ { "params": [ { "name": "path" }, { "name": "atime" }, { "name": "mtime" } ] } ] }, { "textRaw": "fs.watch(filename[, options][, listener])", "type": "method", "name": "watch", "meta": { "added": [ "v0.5.10" ] }, "desc": "

Watch for changes on filename, where filename is either a file or a\ndirectory. The returned object is a fs.FSWatcher.

\n

The second argument is optional. The options if provided should be an object.\nThe supported boolean members are persistent and recursive. persistent\nindicates whether the process should continue to run as long as files are being\nwatched. recursive indicates whether all subdirectories should be watched, or\nonly the current directory. This applies when a directory is specified, and only\non supported platforms (See Caveats).

\n

The default is { persistent: true, recursive: false }.

\n

The listener callback gets two arguments (event, filename). event is either\n'rename' or 'change', and filename is the name of the file which triggered\nthe event.

\n", "miscs": [ { "textRaw": "Caveats", "name": "Caveats", "type": "misc", "desc": "

The fs.watch API is not 100% consistent across platforms, and is\nunavailable in some situations.

\n

The recursive option is only supported on OS X and Windows.

\n", "miscs": [ { "textRaw": "Availability", "name": "Availability", "type": "misc", "desc": "

This feature depends on the underlying operating system providing a way\nto be notified of filesystem changes.

\n\n

If the underlying functionality is not available for some reason, then\nfs.watch will not be able to function. For example, watching files or\ndirectories can be unreliable, and in some cases impossible, on network file\nsystems (NFS, SMB, etc), or host file systems when using virtualization software\nsuch as Vagrant, Docker, etc.

\n

You can still use fs.watchFile, which uses stat polling, but it is slower and\nless reliable.

\n" }, { "textRaw": "Inodes", "name": "Inodes", "type": "misc", "desc": "

On Linux and OS X systems, fs.watch() resolves the path to an inode and\nwatches the inode. If the watched path is deleted and recreated, it is assigned\na new inode. The watch will emit an event for the delete but will continue\nwatching the original inode. Events for the new inode will not be emitted.\nThis is expected behavior.

\n" }, { "textRaw": "Filename Argument", "name": "Filename Argument", "type": "misc", "desc": "

Providing filename argument in the callback is only supported on Linux and\nWindows. Even on supported platforms, filename is not always guaranteed to\nbe provided. Therefore, don't assume that filename argument is always\nprovided in the callback, and have some fallback logic if it is null.

\n
fs.watch('somedir', (event, filename) => {\n  console.log(`event is: ${event}`);\n  if (filename) {\n    console.log(`filename provided: ${filename}`);\n  } else {\n    console.log('filename not provided');\n  }\n});\n
\n" } ] } ], "signatures": [ { "params": [ { "name": "filename" }, { "name": "options", "optional": true }, { "name": "listener", "optional": true } ] } ] }, { "textRaw": "fs.watchFile(filename[, options], listener)", "type": "method", "name": "watchFile", "meta": { "added": [ "v0.1.31" ] }, "desc": "

Watch for changes on filename. The callback listener will be called each\ntime the file is accessed.

\n

The options argument may be omitted. If provided, it should be an object. The\noptions object may contain a boolean named persistent that indicates\nwhether the process should continue to run as long as files are being watched.\nThe options object may specify an interval property indicating how often the\ntarget should be polled in milliseconds. The default is\n{ persistent: true, interval: 5007 }.

\n

The listener gets two arguments the current stat object and the previous\nstat object:

\n
fs.watchFile('message.text', (curr, prev) => {\n  console.log(`the current mtime is: ${curr.mtime}`);\n  console.log(`the previous mtime was: ${prev.mtime}`);\n});\n
\n

These stat objects are instances of fs.Stat.

\n

If you want to be notified when the file was modified, not just accessed,\nyou need to compare curr.mtime and prev.mtime.

\n

Note: when an fs.watchFile operation results in an ENOENT error, it will\n invoke the listener once, with all the fields zeroed (or, for dates, the Unix\n Epoch). In Windows, blksize and blocks fields will be undefined, instead\n of zero. If the file is created later on, the listener will be called again,\n with the latest stat objects. This is a change in functionality since v0.10.

\n

Note: fs.watch() is more efficient than fs.watchFile and fs.unwatchFile.\nfs.watch should be used instead of fs.watchFile and fs.unwatchFile\nwhen possible.

\n", "signatures": [ { "params": [ { "name": "filename" }, { "name": "options", "optional": true }, { "name": "listener" } ] } ] }, { "textRaw": "fs.write(fd, buffer, offset, length[, position], callback)", "type": "method", "name": "write", "meta": { "added": [ "v0.0.2" ] }, "desc": "

Write buffer to the file specified by fd.

\n

offset determines the part of the buffer to be written, and length is\nan integer specifying the number of bytes to write.

\n

position refers to the offset from the beginning of the file where this data\nshould be written. If typeof position !== 'number', the data will be written\nat the current position. See pwrite(2).

\n

The callback will be given three arguments (err, written, buffer) where\nwritten specifies how many bytes were written from buffer.

\n

Note that it is unsafe to use fs.write multiple times on the same file\nwithout waiting for the callback. For this scenario,\nfs.createWriteStream is strongly recommended.

\n

On Linux, positional writes don't work when the file is opened in append mode.\nThe kernel ignores the position argument and always appends the data to\nthe end of the file.

\n", "signatures": [ { "params": [ { "name": "fd" }, { "name": "buffer" }, { "name": "offset" }, { "name": "length" }, { "name": "position", "optional": true }, { "name": "callback" } ] } ] }, { "textRaw": "fs.write(fd, data[, position[, encoding]], callback)", "type": "method", "name": "write", "meta": { "added": [ "v0.11.5" ] }, "desc": "

Write data to the file specified by fd. If data is not a Buffer instance\nthen the value will be coerced to a string.

\n

position refers to the offset from the beginning of the file where this data\nshould be written. If typeof position !== 'number' the data will be written at\nthe current position. See pwrite(2).

\n

encoding is the expected string encoding.

\n

The callback will receive the arguments (err, written, string) where written\nspecifies how many bytes the passed string required to be written. Note that\nbytes written is not the same as string characters. See Buffer.byteLength.

\n

Unlike when writing buffer, the entire string must be written. No substring\nmay be specified. This is because the byte offset of the resulting data may not\nbe the same as the string offset.

\n

Note that it is unsafe to use fs.write multiple times on the same file\nwithout waiting for the callback. For this scenario,\nfs.createWriteStream is strongly recommended.

\n

On Linux, positional writes don't work when the file is opened in append mode.\nThe kernel ignores the position argument and always appends the data to\nthe end of the file.

\n", "signatures": [ { "params": [ { "name": "fd" }, { "name": "data" }, { "name": "position", "optional": true }, { "name": "encoding", "optional": true }, { "name": "callback" } ] } ] }, { "textRaw": "fs.writeFile(file, data[, options], callback)", "type": "method", "name": "writeFile", "meta": { "added": [ "v0.1.29" ] }, "signatures": [ { "params": [ { "textRaw": "`file` {String} filename ", "name": "file", "type": "String", "desc": "filename" }, { "textRaw": "`data` {String | Buffer} ", "name": "data", "type": "String | Buffer" }, { "textRaw": "`options` {Object | String} ", "options": [ { "textRaw": "`encoding` {String | Null} default = `'utf8'` ", "name": "encoding", "type": "String | Null", "desc": "default = `'utf8'`" }, { "textRaw": "`mode` {Number} default = `0o666` ", "name": "mode", "type": "Number", "desc": "default = `0o666`" }, { "textRaw": "`flag` {String} default = `'w'` ", "name": "flag", "type": "String", "desc": "default = `'w'`" } ], "name": "options", "type": "Object | String", "optional": true }, { "textRaw": "`callback` {Function} ", "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "file" }, { "name": "data" }, { "name": "options", "optional": true }, { "name": "callback" } ] } ], "desc": "

Asynchronously writes data to a file, replacing the file if it already exists.\ndata can be a string or a buffer.

\n

The encoding option is ignored if data is a buffer. It defaults\nto 'utf8'.

\n

Example:

\n
fs.writeFile('message.txt', 'Hello Node.js', (err) => {\n  if (err) throw err;\n  console.log('It\\'s saved!');\n});\n
\n

If options is a string, then it specifies the encoding. Example:

\n
fs.writeFile('message.txt', 'Hello Node.js', 'utf8', callback);\n
\n

Note that it is unsafe to use fs.writeFile multiple times on the same file\nwithout waiting for the callback. For this scenario,\nfs.createWriteStream is strongly recommended.

\n" }, { "textRaw": "fs.writeFileSync(file, data[, options])", "type": "method", "name": "writeFileSync", "meta": { "added": [ "v0.1.29" ] }, "desc": "

The synchronous version of fs.writeFile(). Returns undefined.

\n", "signatures": [ { "params": [ { "name": "file" }, { "name": "data" }, { "name": "options", "optional": true } ] } ] }, { "textRaw": "fs.writeSync(fd, buffer, offset, length[, position])", "type": "method", "name": "writeSync", "meta": { "added": [ "v0.1.21" ] }, "desc": "

Synchronous versions of fs.write(). Returns the number of bytes written.

\n", "signatures": [ { "params": [ { "name": "fd" }, { "name": "data" }, { "name": "position", "optional": true }, { "name": "encoding", "optional": true } ] }, { "params": [ { "name": "fd" }, { "name": "buffer" }, { "name": "offset" }, { "name": "length" }, { "name": "position", "optional": true } ] } ] }, { "textRaw": "fs.writeSync(fd, data[, position[, encoding]])", "type": "method", "name": "writeSync", "meta": { "added": [ "v0.11.5" ] }, "desc": "

Synchronous versions of fs.write(). Returns the number of bytes written.

\n", "signatures": [ { "params": [ { "name": "fd" }, { "name": "data" }, { "name": "position", "optional": true }, { "name": "encoding", "optional": true } ] } ] } ], "type": "module", "displayName": "fs" } ] }