1
const { exec } = require('child_process');

I'm still new to node.js. I would like to know what's the purpose of the curly braces near const, is it like an angular/typescript way to fetch the object from the module?

Is there any ES6 or whatever new syntax that I should be aware of? like:

const[foo] = , or const(foo) = 
scharette
  • 9,437
  • 8
  • 33
  • 67
  • This is destructuring assignment - https://stackoverflow.com/questions/15290981/what-does-curly-brackets-in-the-var-statements-do – dzm Oct 27 '17 at 23:51

1 Answers1

1

Yes, this is a part of ES6. They are called named exports and this accessing method is called "destructuring".

So if you have a module with this contents:

export const foo = Math.sqrt(2);

You can use foo by doing any of the below:

import foo from "module";

import { foo } from "module";

import * as mod from "module"; console.log(mod.foo)

Isaiah Turner
  • 2,574
  • 1
  • 22
  • 35
  • 1
    To be clear, `const { exec } = require('child_process')` isn't actually a named export (although it is emulating one); it's just regular destructuring. node doesn't yet have native support for ES6 modules so using destructuring with `require` statements is the next best thing. (Of course, you can use Babel to be able to use the ES6 module syntax.) – Matt Browne Oct 28 '17 at 02:57