1 export const metadata = {
2 title: `Unexpected token 'export' Error`,
3 }
4
5 # {metadata.title}
6
7 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:
8
9 ```bash
10 SyntaxError: Unexpected token 'export'
11 ```
12
13 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`.
14
15 ## Why this Error Occurred
16
17 The Medusa application loads your files, including the configuration file, migrations, API routes, and subscribers, as CommonJS modules at runtime.
18
19 The [build](!docs!/learn/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.
20
21 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.
22
23 ---
24
25 ## How to Diagnose it
26
27 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.
28 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.
29 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.
30
31 ---
32
33 ## How to Fix it
34
35 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:
36
37 ```json title="tsconfig.json"
38 {
39 "compilerOptions": {
40 "module": "Node16",
41 "moduleResolution": "Node16"
42 }
43 }
44 ```
45
46 Then, if your `package.json` file has a `"type": "module"` field, remove it:
47
48 ```json title="package.json"
49 {
50 "type": "module"
51 }
52 ```
53
54 <Note type="warning">
55
56 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](https://nodejs.org/api/packages.html#type).
57
58 </Note>
59
60 Finally, create the production build again:
61
62 ```bash npx2yarn
63 npx medusa build
64 ```
65
66 The compiled files now use CommonJS syntax, and you can run migrations and start the application as explained in the [build](!docs!/learn/build) documentation.
67
68 ---
69
70 ## Additional Resources
71
72 - [Build Medusa Application](!docs!/learn/build): Learn how to create a production build and start it.
73 - [General Deployment](!docs!/learn/deployment/general): Learn the general steps to deploy a Medusa application.