Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 426 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
module.exports vs. export default in Node.js and ES6

#1
What is the difference between Node's `module.exports` and ES6's `export default`? I'm trying to figure out why I get the "__ is not a constructor" error when I try to `export default` in Node.js 6.2.2.

### What works

'use strict'
class SlimShady {
constructor(options) {
this._options = options
}

sayName() {
return 'My name is Slim Shady.'
}
}

// This works
module.exports = SlimShady

### What _doesn't_ work

'use strict'
class SlimShady {
constructor(options) {
this._options = options
}

sayName() {
return 'My name is Slim Shady.'
}
}

// This will cause the "SlimShady is not a constructor" error
// if in another file I try `let marshall = new SlimShady()`
export default SlimShady
Reply

#2
The issue is with

- how ES6 modules are emulated in CommonJS
- how you import the module

### ES6 to CommonJS

At the time of writing this, no environment supports ES6 modules natively. When using them in Node.js you need to use something like Babel to convert the modules to CommonJS. But how exactly does that happen?

Many people consider `module.exports = ...` to be equivalent to `export default ...` and `exports.foo ...` to be equivalent to `export const foo = ...`. That's not quite true though, or at least not how Babel does it.

ES6 `default` exports are actually also *named* exports, except that `default` is a "reserved" name and there is special syntax support for it. Lets have a look how Babel compiles named and default exports:

// input
export const foo = 42;
export default 21;

// output
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
var foo = exports.foo = 42;
exports.default = 21;

Here we can see that the default export becomes a property on the `exports` object, just like `foo`.

## Import the module

We can import the module in two ways: Either using CommonJS or using ES6 `import` syntax.

**Your issue:** I believe you are doing something like:

var bar = require('./input');
new bar();

expecting that `bar` is assigned the value of the default export. But as we can see in the example above, the default export is assigned to the `default` property!

So in order to access the default export we actually have to do

var bar = require('./input').default;

If we use ES6 module syntax, namely

import bar from './input';
console.log(bar);

Babel will transform it to

'use strict';

var _input = require('./input');

var _input2 = _interopRequireDefault(_input);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

console.log(_input2.default);

You can see that every access to `bar` is converted to access `.default`.
Reply

#3
You need to configure babel correctly in your project to use export default and export const foo

npm install --save-dev @babel/plugin-proposal-export-default-from

then add below configration in .babelrc

"plugins": [
"@babel/plugin-proposal-export-default-from"
]
Reply

#4
Felix Kling did a great comparison on those two, for anyone wondering how to do an export default alongside named exports with module.exports in nodejs
``` javascript
module.exports = new DAO()
module.exports.initDAO = initDAO // append other functions as named export

// now you have
let DAO = require('_/helpers/DAO');
// DAO by default is exported class or function
DAO.initDAO()
```




Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through