JavaScript Promises: 6/11 - Why is checkInventory exported, and imported, with curly braces around the variable

In the lesson about exporting and importing modules we learned that when we export with module.exports, we can for example export an object, let’s say an object called House, like this:

module.exports = House;

And to import it with require(), we type:

const House = require(’./filePath’);

BUT, in the JavaScript Promises 6/11 lesson, they put curly braces around the variable name checkInventory when they export it with module.exports and import it with require()…

module.exports = { checkInventory };

const {checkInventory} = require(’./library.js’);

Why the curly braces? Is it a typo?

I was curious about this too, so I looked it up and found this:

Basically, you can leave out the curly braces if it’s an default export, but you have to use them if it’s a named export. The link gives some examples of the difference, and why you might use one or the other.

1 Like

Yes this is for default export and named exports. But the one Im confused about is Node’s module.export and require() that they’ve used in the lesson where they export the checkInventory function and wrap around the curly braces around the variable name, both when they export the function and when they import it.

BUT, I took a closer look at my notes, and the Codecademy lesson about modules, and saw that you CAN use the curly braces, when using Node’s module.exports, but that is when you want to export several different data, just like with named exports. BUT in the lesson we only export the checkInventory function, so what’s the point of the braces if there’s no other data we want to export?

When it comes to importing with Node’s require(), I took a look at my notes and the Codecademy lesson about modules, and I see no curly braces around the name of the module we are importing, no matter if the module we exported with Node’s module.exports consist of a single type of data, or several different data…so why do they use the curly braces around checkInventory when importing it, when nowhere in the Codecademy lesson about modules, do they use curly braces when importing with require()?

If you you look at part 2(module.exports), 3(require()) and 4(module.exports II) in lesson 11(Intermediate JacaScript Modules), you’ll see what I mean.

You can even see in the example of part 4(module.exports II) of the lesson, how they export the different data, with the curly braces, which is fine, but when they import the data, you can clearly see that there are no curly braces around the Menu module

module.exports II

We can also wrap any collection of data and functions in an object, and export the object using module.exports . In menu.js , we could write:

module.exports = { specialty: “Roasted Beet Burger with Mint Sauce”, getSpecialty: function() { return this.specialty; } };

In the above code, notice:

  1. module.exports exposes the current module as an object.
  2. specialty and getSpecialty are properties on the object.

Then in order.js , we write:

const Menu = require(’./menu.js’); console.log(Menu.getSpecialty());

Here we can still access the behavior in the Menu module.

BTW, in the name of this topic I ment to write 6/12 and not 6/11. 11 is the Modules lesson, and 6/12 is the Promises lesson that gets me confused with the curly braces :slight_smile:

According to the difference between import / export and require/module.exports is that the former is ES6 (not supported by Node, but supported by many browsers), and the latter is older syntax, which Node understands.

ES6 uses the curly braces, and the “older” Node version of Javascript does not (unless you’re exporting an object directly in the export statement, as in lesson 4).

Lesson 2/3: “Vanilla” syntax: module.export with require()
Lesson 4: Instead of naming the module/object in the file and exporting it by name (Menu), they just export the object directly in the export statement and give it a name in the import file.

There’s an important caveat in lesson 5:

When using ES6 syntax, we use export default in place of module.exports . Node.js doesn’t support export default by default, so module.exports is usually used for Node.js development and ES6 syntax is used for front-end development. As with most ES6 features, it is common to transpile code since ES6 is not supported by all browsers.

By the time you get to Lesson 11, they’ve been working with the ES6 syntax, which uses the curly braces in almost all the import / export statements.

1 Like

Yes that is correct, I finished the whole lesson last week or something, so I get that part. But I still want to know why the curly braces in the Codecademys Promises lesson, where they use Node’s module.exports and require() thats all. And the reason I want to know that, is if let’s say I happen to work with Node one day, and actually want to use module.exports and require(). I would like to know the right syntax for that, with other words, I would like to know when I should use the curly braces when exporting and importing with module.exports and require().

Okay, sorry for being so thick. I was looking at the Modules lessons and not the Promises lesson. I see now what you’re saying.

I found this: , where the curly braces are for destructuring, but it wouldn’t seem to apply when you’re only exporting a single function. I couldn’t find any other references to using curly braces with the ‘old’ module.exports syntax.

Maybe they put the curly braces in as a destructuring tool by force of habit or ‘best practices.’ Doesn’t hurt anything to have them there, even if you don’t need to destructure.

Did you try removing the curly braces in the Promises lesson to see if the app still worked?

1 Like

When I removed the curly braces in one file (the export statement) the app broke, but when I removed them in both the import and export statements, it worked.

1 Like

Oh ok. Well, then I guess, If I happen to use Node one day, I’ll only use the curly braces when I use module.export to export a collection of data, and nowhere else, just like they did in the 4/11 lesson :wink:

Thank you for all your help, I appreciate it :slight_smile:

De nada. I learned a lot researching your question, even if ultimately I couldn’t really answer it. For me, the takeaway is: Curly braces for all, everywhere! Because: why not?

1 Like