ES Modules: Using Named Exports as the Default Export

// in `cow.js` :
export function speak () {
return 'moo'

To use a named export, we can import it directly into our code:

// in `import-named.js` :
import { speak } from './cow';

speak() // => 'moo'

Aliasing named imports

You can assign a new name to a named export as you import it,
allowing you to resolve naming collisions,
or give the export a more informative name.

// in `import-named-aliased.js` :
import { speak as cowSpeak } from './cow.js';
import { speak as goatSpeak } from './goat.js';
// import { speak } from './goat.js'; // Error: Duplicate declaration "speak"

cowSpeak() // moo
goatSpeak() // baa

Namespace Imports

Thankfully we don’t need to namespace everything by hand,
we can use the import * as X syntax
to pull all of the named exports into a local namespace.

// in `import-namespace.js` :
import * as cow from './cow.js'
import * as goat from './goat.js'

cow.speak() // moo
goat.speak() // baa

What is difference between module.exports and export in node.js?
module.exports & exports are synonymous. No difference.
Why have 2 things to perform the same task?
The answer is convenience & simplicity of code.

If, say, you have to export 3 functions: get (), put (), post (), here’s what you’re going to do:

module.exports.get = function () {…}; = function () {…};
module.exports.put = function () {…};

Now, here’s the same code using exports :

exports.get = function () {…}; = function () {…};
exports.put = function () {…};

When you only have to export 1-item from your script without giving it a name, you use :

// in `yourScriptName.js` :
module.exports = myObject

Then the user of your script uses myObject like:

var hisObject = require ('./yourScriptName');
console.log (hisObject); //prints your object without a name

But if you plan to expose that 1-item through a name, then you can do something like:

// in `yourScriptName.js` :
exports.ExportedObject = myObject;

Then, your user can use it like:

var hisObjectScript = require ('./yourScriptName');
console.log (hisObjectScript.ExportedObject); // print your object through a name, `ExportedObject`

module.exports vs. export default in Node.js and ES6

input.js module:

module.exports = Food;

export default Water;

Import the input module:

"use strict";

// use `require` :
var Food = require('./input');
var Water = require('./input').default;

// If we use ES6 module syntax :
import Food from './input';
import Water from './input';

As a guideline, if the relationship between exports and module.exports
seems like magic to you, ignore exports and only use module.exports.

exports = module.exports = {}

// module.exports.method = function() {...}
exports.method = function() {...}

Here are some good and some bad examples:

exports is NOT returned by require() (but module.exports is!)

// calculator-exports-examples.js

// good
module.exports = {
add(a,b) { return a+b }

// good
module.exports.subtract = (a,b) => a-b

// valid
exports = module.exports

// good and simply a shorter version of the code above
exports.multiply = (a,b) => a*b

// bad, exports is never exported
exports = {
divide(a,b) { return a/b }

Leave a Reply