Last updated: Mar 2, 2024
Reading timeยท4 min
The "Uncaught SyntaxError Unexpected token 'export'" occurs for 2 main reasons:
type
to
module
in package.json
.type
to module
in
the script tag.Here's an example of how the error occurs in a Node.js application.
// โ๏ธ Uncaught SyntaxError: Unexpected token 'export' export class Person { constructor(first) { this.first = first; } }
type
property to module
in your package.json
fileTo solve the error, set the type
property to module
in your package.json
file.
Files ending with a .js
extension are loaded as ES6 modules when the nearest
package.json
has a type
field set to module
.
If you don't have a package.json
file, you can create one with the
npm init -y
command.
Open your terminal in your project's root directory and run the following command.
npm init -y
Now set the type
property to module
in the package.json
file:
{ "type": "module", // ... rest }
Now you can use the ES6 modules import/export syntax in your Node.js application.
This is a file called index.js
that has 2 named exports.
// ๐๏ธ named export export class Person { constructor(first) { this.first = first; } } // ๐๏ธ named export export const site = 'bobbyhadz.com';
And here's how we would import the class and variable into a file called
another-file.js
.
import {Person, site} from './index.js'; const p1 = new Person('James'); console.log(p1); // ๐๏ธ Person { first: 'James' } console.log(site); // ๐๏ธ bobbyhadz.com
Make sure to specify the extension of the file in your import statements.
type
property to module
in the package.json
file of your project allows you to use ES6 modules in your Node.js application.If you don't want to set the type
property to module
in your package.json
,
you can use the older CommonJS syntax.
You can also solve the error by refactoring your code to use the CommonJS
syntax, e.g. module.exports = {num};
instead of export num = 10;
.
class Person { constructor(first) { this.first = first; } } // โ Using module.exports instead of export module.exports = { Person, };
Then we can import members of other files using require()
.
const {Person} = require('./index.js');
Make sure that you haven't set the type
attribute to module
in your
package.json
file and you don't have files with a .mjs
extension to be able
to use the require()
syntax.
There are multiple ways around it, e.g. to use Babel to transpile our ES6 code to an older version of JavaScript, or simply refactor our code to the CommonJS syntax.
To solve the error, set the type
of your <script />
tags to module
.
The type="module"
attribute is supported in all modern browsers and allows us
to use the ES6 modules syntax.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <!-- โ type is set to `module` โ --> <script type="module" src="index.js"></script> </body> </html>
Setting type
to module
enables us to use the ES6 modules syntax in the
index.js
file.
// ๐๏ธ named export export class Person { constructor(first) { this.first = first; } } // ๐๏ธ named export export const site = 'bobbyhadz.com';
type
attribute to module
when loading your scripts, you can use the ES6 modules syntax.You can then import the exports in the following way.
// ๐๏ธ named imports import {Person, site} from './index.js'; console.log(Person); // ๐๏ธ [class Person] console.log(site); // ๐๏ธ "bobbyhadz.com"
Note that you have to set the type
attribute to module
on all scripts that
use the ES Modules syntax.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <!-- โ type is set to `module` on both scripts โ --> <script type="module" src="index.js"></script> <script type="module" src="another-file.js"></script> </body> </html>
type="module"
attribute.named
exports and importsHere are some examples of using the ES6 modules syntax with named
and
default
exports.
This is a file called index.js
that has 3 named exports.
// ๐๏ธ named exports export class Person {} export class Animal {} export const site = 'bobbyhadz.com'
Now we can import the exports into a file called another-file.js
.
// ๐๏ธ named imports import {Person, Animal, site} from './index.js'; console.log(site); // ๐๏ธ bobbyhadz.com
And here is an example of a file that uses a default
export.
// ๐๏ธ default export export default class Person {}
default
export per file, however, you can have as many named
exports as necessary.default
export and importHere is how we would import the default
export.
// ๐๏ธ default import import Person from './index.js'
Notice that we don't use curly braces with default
imports.
You can also mix and match. This is a file called index.js
that uses a default
and a named export.
// ๐๏ธ default export export default class Person {} // ๐๏ธ named export export const age = 30;
And here is how we can import the class and variable into another-file.js
.
// ๐๏ธ default and named imports import Person, {age} from './index.js'
Things to note:
default
export per file, but you can have as
many named
exports as necessary.To solve the "SyntaxError: Unexpected token 'export'" error, make sure:
type
to module
in your package.json
file in Node.js.type
to module
on your JS script tags in the browser.