Search code examples
node.jsconstantsbraces

I didn't understand const{} in Node


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) = 

Solution

  • 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)