Node.js Filesystem I/O edit

Reading from a file synchronously

For any file operations, you will need the filesystem module:

const fs = require('fs');

Reading a String

fs.readFileSync behaves similarly to fs.readFile, but does not take a callback as it completes synchronously and therefore blocks the main thread. Most node.js developers prefer the asynchronous variants which will cause virtually no delay in the program execution.

If an encoding option is specified, a string will be returned, otherwise a Buffer will be returned.

// Read a string from another file synchronously
let content;
try {
  content = fs.readFileSync('sync.txt', { encoding: 'utf8' });
} catch(err) {
  // An error occurred
  console.error(err);
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Table Of Contents
2 npm
4 Filesystem I/O
42 cli
60 Hack
65 ES6
92 CORS
106 N-API
  ↑ ↓ to navigate     ↵ to select     Esc to close