Your project is not set up properly to interpret the file that has the error as an ESM module file. It is, instead, being interpreted as a CommonJS module (where you use require()
, not import
) which is the nodejs default.
There are a number of ways to tell nodejs that your file is supposed to be an ESM module.
- You can give it a file extension of
.mjs
.
- You can add
"type": "module"
to your package.json file.
- You can use the
--input-type=module
command line argument when starting node.js if this file is your top level file you're executing.
See nodejs doc here on this subject.
FYI, you could just switch to the CommonJS syntax of using require()
and module.exports
instead of import
and export
, but I assume what you really want to do is to tell nodejs that you want to use ESM modules so you can use the more modern import
and export
syntax.