www/apps/resources/app/troubleshooting/esm-syntax-error/page.mdx
export const metadata = {
title: 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:
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.
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.
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.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..medusa/server and check whether it starts with import or contains export statements. If it does, the compiled output uses an ES module format.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:
{
"compilerOptions": {
"module": "Node16",
"moduleResolution": "Node16"
}
}
Then, if your package.json file has a "type": "module" field, remove it:
{
"type": "module"
}
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:
npx medusa build
The compiled files now use CommonJS syntax, and you can run migrations and start the application as explained in the build documentation.