question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Loses inline functions in object assigned to module.exports

See original GitHub issue

The modules.commonjs plugin doesn’t seem to understand overwriting module.exports

module.exports = {
    controller : function() { },
    view : function() { }
};

// converts into
export { undefined as controller, undefined as view };

If I instead directly assign to properties on the exports object it does the right thing.

exports.controller = function() { };
exports.view = function() { };

// converts into
export function controller() { }
export function view() { }

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
rnubelcommented, Jun 8, 2016

Related to this, an export of a single function fails with a stacktrace:

// test.js
module.exports = {
  foo: function() { return; }
}
$ esnext test.js
TypeError: Cannot read property 'name' of undefined
    at rewriteSingleExportAsDefaultExport (/Users/robert/.nvm/versions/node/v6.1.0/lib/node_modules/esnext/dist/esnext.umd.js:1677:43)
    at rewriteAsExport (/Users/robert/.nvm/versions/node/v6.1.0/lib/node_modules/esnext/dist/esnext.umd.js:1630:14)
    at /Users/robert/.nvm/versions/node/v6.1.0/lib/node_modules/esnext/dist/esnext.umd.js:1609:14
    at Array.forEach (native)
0reactions
sheeruncommented, Jul 31, 2016

It’s the same with simple numbers:

module.exports = {
  foo: 12,
  bar: 13
};
export { undefined as foo, undefined as bar };
Read more comments on GitHub >

github_iconTop Results From Across the Web

module.exports vs exports in Node.js - Stack Overflow
Basically node.js doesn't export the object that exports currently references, but exports the properties of what exports originally references.
Read more >
Module exports in Node.js explained - Java Brains - YouTube
Have you seen the new Java Brains? Check out www.javabrains.io now for awesome courses and content!Learn how to export your objects and ...
Read more >
Node Module Exports Explained - freeCodeCamp
This is a common module exports mistake that people who are starting out with Node.js often make. They assign exports to a new...
Read more >
16. Modules - Exploring JS
An ES6 module can pick a default export, the main exported value. Default exports are especially easy to import. The following ECMAScript 6...
Read more >
Understanding module.exports and exports in Node.js
Here we're assigning the functions and values we want to export to an exports property on module — and of course, this works...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found