• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Node js – Issue with my syntax in require(module) . When to use {} and when not to use {} in require(‘module’)

Resolved: Node js – Issue with my syntax in require(module) . When to use {} and when not to use {} in require(‘module’)

0
By Isaac Tonny on 18/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I have a query with the syntax in the require statement. Please refere the sample code below.
Some times , I see sample codes which use
Other times, I see like below
What is the difference between them? Thanks in Advance.
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:
is a shortcut for this:
So, within this context, you use the { google } only when you want the .google property from the imported module.
If you want the entire module handle such as this:
then, you don’t use the { }. 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!

node.js
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: Time Complexity: Find the number of people who know a secret leetcode 2327

24/03/2023

Resolved: Best way to create 3d matrix of variables in PULP

24/03/2023

Resolved: Openlayers: Update clusters depending on filter (geometryFunction)

24/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.