81534819 scenario Reproduction
a.js
export let test = function () { console.log(‘1‘);}
b.js
let a= require (‘./a‘);a.test();
Runnode b
The following error occurs:
export default {^^^^^^SyntaxError: Unexpected token export
Solution
a.js
Change to the following:
exports.test = function () { console.log(‘1‘);}
Root Cause
Node and browser support different module specifications.
Entries |
Node |
Browser |
Module specification |
Commonjs |
Es6 |
Export |
* modules.exports ; Exports |
Export; export default |
Introduction |
Require |
Import; Require |
1. about exports and module. Exports
- When a node executes a file, it generates an exports and module object for the file,
The module has an exports attribute.
- Exports = module. Exports = {};
2. About export and export default
- Both export and export default can be used to export constants, functions, files, modules, etc.
- In a file or module, there can be multiple export and import, and export default has only one
- Export via export. Add {} during import, and export default is not required
- Export can directly export the variable expression, but export default cannot.
Convert exports and module. Exports