1

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

Doby
  • 91
  • 3
  • 10
  • 1
    You need the `.mjs` extension 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. – Shilly Oct 03 '19 at 14:15

1 Answers1

4

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
ahwayakchih
  • 2,101
  • 19
  • 24