Shaffanhoon Shaffanhoon - 1 year ago 144
Node.js Question

Curly brackets in node require statement

I am trying to figure out what the difference is between the two require statements below.

What is the function of the

wrapped around the constant?

const electron = require('electron')

const {ipcMain} = require('electron')

They both appear to assign the contents of the electron module but they function differently.

Can anyone shed some light?

Answer Source

The second example uses destructuring.

This will call the specific variable (including functions) that are exported from the required module.

For example (functions.js):

module.exports = {

is included in your file:

const { func1, func2 } = require('./functions')

Now you can call them individually,


as opposed to:

const Functions = require('./functions')

are called using dot notation:


Hope this helps.

You can read about destructuring here, it is a very useful part of ES6 and can be used with arrays as well as objects.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download