@mtf @midlindner
In this exercise the function checkInventory
is exported from one file, and imported into another, using the following code:
module.exports = { checkInventory };
const { checkInventory } = require('./library.js');
I’m not sure why the function name checkInventory
is wrapped in curly braces.
In the lesson Intermediate Javascript Modules, we learnt in Exercise 2. module.exports and Exercise 3. require( ) that the name of the module being exported/imported is written with a capital letter and without curly braces. Then in Exercise 4. module.exports II we learnt that we do use curly braces in module.exports = { };
when we directly assign the data and functions (which we’re exporting) to module.exports
as properties of an object which we create with the curly braces — instead of exporting them by reference to a previously defined module name. However, I do realise that neither of these situations really matches the one we have in this exercise where we are exporting a single, pre-defined function by reference to its function name.
Are the curly braces actually necessary? The function is successfully exported/imported, and everything seems to work just as well, when I run the same code but without the curly braces.
In later exercises in the Intermediate Javascript Modules lesson, we learnt that we do wrap the variable names of the data we are exporting/importing in curly braces when these are separately named exports. At first, this would seem to explain why we use curly braces to export/import the function in this exercise; however, in the modules lesson, the curly braces are used in this way when we are exporting/importing using ES6 syntax, which is different from the Node.js syntax we are using here.
In this exercise, are the curly braces used with module.exports
because we are exporting a single function, rather than a pre-defined module? That would seem strange to me because modules seem to be defined in exactly the same way as objects (only with a name that starts with a capital letter) and, after all, functions are also objects.
Do we only start the name/identifier with a capital letter when exporting the whole file as a pre-defined module? Can this pre-defined module only be an object literal i.e. not a function? Is that why we don’t use a capital letter when exporting/importing the function in this exercise? Even if there was only one function in the file, and we exported that, would we still not use a capital letter for this same reason?
All in all, I’m still struggling to understand what’s actually going on here in terms of the curly braces and capital letters. Could someone shed some light on this?