Skip to content

Instantly share code, notes, and snippets.

@brianleroux
Last active May 6, 2024 09:04
Show Gist options
  • Save brianleroux/6c649f3a2cfa4edea296 to your computer and use it in GitHub Desktop.
Save brianleroux/6c649f3a2cfa4edea296 to your computer and use it in GitHub Desktop.
// Node style module writ in ES6 syntax:
exports default foo = () => console.log(‘hi’)
// Unfortunately “literally” transpiles to this:
exports.default = function foo() {
return console.log(‘hi’)
}
// Which means this will fail:
var foo = require(‘./foo’)
foo() // object is not a function!
// But this will work:
var foo = require(‘./foo’).default
foo() // outputs 'hi'
// There is a fix!
//
// If we compile with 6to5 we get what we expect!
module.exports = function foo() {
return console.log(‘hi’)
}
// call as usual
var foo = require(‘./foo’)
foo() // outputs 'hi'!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment