Skip to content

Is module.exports = { fn } same as exports.fn = fn

For job test i need to create library like this:

// do-it.js
function doIt(smth) {}

module.exports = {
    doIt,
};

But i’m working with typescript and compiled do-it.ts file looks like this:

// do-it.js when compiled from do-it.ts
exports.__esModule = true;
exports.doIt= exports.another = void 0;

function doIt(smth) {}
exports.doIt = doIt;

function another() {}
exports.another= another;

Is exports from this two examples will work the same?

Answer

Roughly speaking, yes; certainly the resulting exports are the same.

I mean, if you did:

module.exports = {
    doIt,
};

and later did

module.exports = {
    doSomethingElse,
};

you’d have a problem, because the second one completesly replaces the previous exports object.

You don’t need to create that object at all, it’s created for you before your module is called. So really, you can just do

exports.doIt = doIt;

and then

exports.doSomethingElse = doSomethingElse;

in the first place.