Unexpected token 'export' Error

You may run into the following error when you run migrations or start the Medusa application from the .medusa/server directory, such as during deployment:

Terminal
❯SyntaxError: Unexpected token 'export'

The error points to a compiled JavaScript file in the .medusa/server directory, such as .medusa/server/medusa-config.js or a file under .medusa/server/src.

Why this Error Occurred#

The Medusa application loads your files, including the configuration file, migrations, API routes, and subscribers, as CommonJS modules at runtime.

The build command compiles your TypeScript files using the module option in your tsconfig.json file. If you set that option to an ES module format, such as ESNext or ES2022, the compiled files keep the export and import keywords instead of using CommonJS syntax.

Node.js then treats every .js file as CommonJS, since the package.json file of a Medusa application doesn't have a "type": "module" field. So, Node.js fails to parse the export keyword and throws the above error.


How to Diagnose it#

  1. Open the tsconfig.json file at the root of your Medusa application and check the module and moduleResolution options. An ES module format, such as ESNext, ES2020, or ES2022, causes this error.
  2. Open the package.json file at the root of your Medusa application and check whether it has a "type": "module" field. If it does, remove it using the steps in the next section.
  3. Open the file mentioned in the error message under .medusa/server and check whether it starts with import or contains export statements. If it does, the compiled output uses an ES module format.

How to Fix it#

To fix this error, change the module and moduleResolution options in your tsconfig.json file to Node16, which is the configuration that Medusa applications use:

tsconfig.json
1{2  "compilerOptions": {3    "module": "Node16",4    "moduleResolution": "Node16"5  }6}

Then, if your package.json file has a "type": "module" field, remove it:

package.json
1{2  "type": "module"3}
Warning: Don't add a "type": "module" field to your package.json file to resolve this error. It changes how Node.js interprets every file in your project, and Medusa applications don't support ES modules. Learn more about this field in the Node.js documentation.

Finally, create the production build again:

The compiled files now use CommonJS syntax, and you can run migrations and start the application as explained in the build documentation.


Additional Resources#

Was this page helpful?
Ask Bloom
For assistance in your development, use Claude Code Plugins or Docs MCP server in Cursor, VSCode, etc...FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break
⇧↵