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

1
  • 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. Commented Oct 3, 2019 at 14:15

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.