I started reading the section on Node.js in the online copy of Eloquent JavaScript (BTW, a well-done book). All examples use the following construct to capture the result of a require()
call:
const {fs} = require("fs");
If I try to reproduce this in my node.js 8.9.3 64 bits, the resulting fs
variable is undefined. A more usual construct:
const fs = require("fs");
instead returns a usable variable.
My question. I'm missing something obvious? (Maybe I simply have to read the book from the beginning...). Which benefits the first construct gives instead of the more normal variable definition? Thanks!
In ES6, you can destruct objects like this:
const obj = {first: 1, second: 2};
const {first, second} = obj;
console.log(first,second); // 1 2
Similarly, Node's fs
has a member readFile
that is being used like:
const {readFile} = require('fs');