person.js
export default class Person {
// code...
}
main.js
import Person from './person';
When run main.js i got an error
import Person from './person';
^^^^^^
SyntaxError: Unexpected identifier
Thanks
Node has experimental support for ECMAScript Modules: https://nodejs.org/api/esm.html
If you insist on using that syntax, rename files with .mjs extension, instead of .js and run node with --experimental-modules flag, like this:
node --experimental-modules main.mjs
Using Node.js v12+, you can keep .js file extensions if you set "type": "module" in package.json file, in which case command to run it will be:
node --experimental-modules main.js
.mjsextension and the experimental modules flag in all but maybe the latest versions of node. This also means you are forced to use the extension of the file.