As a web developer, you may confused with these module import/export usages:
exports.xx = xx
module.exports = xx
export xx
export default xx
require('..')
orimport xx from '..'
?
Here are a brief instruction of these two module format usages:
CommonJs module format
Named exports
1 | # name export demo.js |
Module.exports
1 | # demo.js |
Actually, the module
is like this:
1 | { |
the module.exports
=== exports
in exports.name1
and exports.name2
, when we use require('demo.js')
to import a module, it is same as:
1 | const obj = module.exports |
ES6 module
ES6 uses export
instead of exports
:
1 | # demo.js |
Of course it supports default export:
1 | # demo.js |
You could even mix the default export and named export:
1 | # demo.js |