Getting Unexpected Token Export

Updated for 2022

You are using ES6 Module syntax.

This means your environment (e.g. node v14.13.0 or newer) must support ESM (Ecmascript Module Syntax).

NodeJS since v14.13.0 supports EcmaScript Module Syntax but it must be enabled by adding the property "type":"module" to package.json. NodeJS versions prior to v14.13.0 uses CommonJS Module syntax by default (module.exports), not ES6 module syntax (export keyword).

Solutions:

  • Enable module support in package.json as outlined above, if
  • Refactor with CommonJS syntax (for older versions of NodeJS)
  • Accept that TypeScript is just better and write .ts files along with ts-node or ts-node-dev npm package
  • (deprecated) Use babel npm package to transpile your ES6 to a commonjs target

Javascript Module standards have changed a lot in recent years, so it can be a bit confusing.

Leave a Comment