Question:
I have a query with the syntax in the require statement. Please refere the sample code below.Grateful to the Developers Community.
Answer:
So, you use{ }
in this context when you want to do object destructuring to get a property from the exported object and create a module-level variable with that same name.This:
{ google }
only when you want the .google
property from the imported module.If you want the entire module handle such as this:
{ }
. The only way to know which one you want for any given module is to consult the documentation for the module, the code for the module or examples of how to use the module. It depends entirely upon what the module exports and whether you want the top level export object or you want a property of that object.It’s important to realize that the
{ }
used with require()
is not special syntax associated with require()
. This is normal object destructuring assignment, the same as if you did this:If you have better answer, please add a comment about this, thank you!