The SyntaxError: Unexpected token ‘export’ in JavaScript is an error that is not difficult to fix, so let’s read this article. I will give some specific examples and some ways to fix it.
Why does the SyntaxError: Unexpected token ‘export’ in JavaScript happen?
There are many cases when your program throws this error, and the most common one is that you forgot to set the type attribute or the type property in the package.json file.
The error message occurs as follows:
Uncaught SyntaxError: Unexpected token 'export'
Example:
export class Product { constructor(name, price) { this.first = first; this.price = price; } }
Output:
Uncaught SyntaxError: Unexpected token 'export'
How to fix this error?
Change script tag attribute
For your program, to no longer output the above error line, make sure that you have changed the type attribute of the script tag to a module so that your program will understand and output data to another file.
Example:
index.html
<!DOCTYPE html> <html> <head> <title>Demo</title> <link rel="stylesheet" href="style.css"> </head> <body> <h1>Hello LearnShareIT</h1> <!-- set type property is module --> <script type="module" src="main.js"></script> <script type="module" src="Example.js"></script> </body> </html>
main.js
export class Product { constructor(name, price) { this.first = first; this.price = price; } }
Example.js
import { Product } from "./main.js"; console.log(Product);
Output:
class Product {
constructor(name, price) {
this.first = first;
this.price = price;
}
}
After I change the type attribute to the module of the script tag, we can pass the Product class information from the main.js file to Example.js.
Set the type attribute in the file package.json
If your program has a package.json file, you can simplify it using the “npm init -y” command.
After having the package.json file, we set the type attribute in the package.json file and then execute the code to export and import.
Example:
package.json
{ "type": "module", ... }
main.js
export class Product { constructor(name, price) { this.first = first; this.price = price; } }
Example.js
import { Product } from "./main.js"; console.log(Product);
Output:
class Product {
constructor(name, price) {
this.first = first;
this.price = price;
}
}
My error is gone for this way, commonly due to people use node.js or use this method. This is also an effective way to fix this error.
Summary
In the above article, I showed you how to fix the SyntaxError: Unexpected token ‘export’ in JavaScript. Try following me after reading the article. It will help you understand and quickly fix it if you see it again. I hope this article helps you and your program. Good luck!
Maybe you are interested:
- SyntaxError: Illegal break statement in JavaScript
- “SyntaxError: Illegal return statement” in JavaScript
- SyntaxError: Unexpected token in JavaScript

My name is Tom Joseph, and I work as a software engineer. I enjoy programming and passing on my experience. C, C++, JAVA, and Python are my strong programming languages that I can share with everyone. In addition, I have also developed projects using Javascript, html, css.
Job: Developer
Name of the university: UTC
Programming Languages: C, C++, Javascript, JAVA, python, html, css