Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Unexpected Token Export

I am trying to run some ES6 code in my project but I am getting an unexpected token export error.

export class MyClass {
  constructor() {
    console.log("es6");
  }
}
like image 447
Jason Avatar asked Jul 10 '16 21:07

Jason


People also ask

How do I fix unexpected token export?

To solve the "Uncaught SyntaxError Unexpected token 'export'" 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.

How do I get an unexpected token?

As you write your JavaScript application, the unexpected token error always occurs because JavaScript expected a specific syntax that's not fulfilled by your current code. You can generally fix the error by removing or adding a specific JavaScript language symbol to your code.

Does not provide an export named?

The error "The requested module does not provide an export named" occurs when mixing up default and named ES6 module imports and exports. To solve the error make sure to import default exports without using curly braces and import named exports with curly braces.

Does not provide an export named default?

To solve the error "The requested module does not provide an export named 'default'", use the default keyword when exporting a value from a file and don't wrap the corresponding import in curly braces. You can only have a single default export per file.


4 Answers

Updated for 2022

You are using EcmaScript Module (ESM or 'ES6 Modules') syntax but your environment does not support it.

NodeJS versions prior to v14.13.0 do not support ESM (export keyword syntax) and use CommonJS Modules (module.exports property syntax). NodeJS v14.13.0 and newer supports ESM but it must be enabled first.

Solutions:

  • If you are using NodeJS v14.13.0 or newer (which does support ESM) you can enable it by setting "type":"module" in your project package.json
  • Refactor with CommonJS Module syntax (for older versions of NodeJS)
  • Consider using TypeScript alongside ts-node or ts-node-dev npm packages (for instant transpilation at development time) and write TypeScript in .ts files
  • Transpile ESM to CommonJS using esbuild (esbuild package on npm) configured to transpile your ES6 javascript to a CommonJS target supported by your environment. (babel is no longer recommended)
like image 164
Phil Ricketts Avatar answered Oct 24 '22 16:10

Phil Ricketts


In case you get this error, it might also be related to how you included the JavaScript file into your html page. When loading modules, you have to explicitly declare those files as such. Here's an example:

//module.js:
function foo(){
   return "foo";
}

var bar = "bar";

export { foo, bar };

When you include the script like this:

<script src="module.js"></script>

You will get the error:

Uncaught SyntaxError: Unexpected token export

You need to include the file with a type attribute set to "module":

<script type="module" src="module.js"></script>

then it should work as expected and you are ready to import your module in another module:

import { foo, bar } from  "./module.js";

console.log( foo() );
console.log( bar );
like image 27
Wilt Avatar answered Oct 24 '22 16:10

Wilt


My two cents

Export

ES6

myClass.js

export class MyClass1 {
}
export class MyClass2 {
}

other.js

import { MyClass1, MyClass2 } from './myClass';

CommonJS Alternative

myClass.js

class MyClass1 {
}
class MyClass2 {
}
module.exports = { MyClass1, MyClass2 }
// or
// exports = { MyClass1, MyClass2 };

other.js

const { MyClass1, MyClass2 } = require('./myClass');

Export Default

ES6

myClass.js

export default class MyClass {
}

other.js

import MyClass from './myClass';

CommonJS Alternative

myClass.js

module.exports = class MyClass1 {
}

other.js

const MyClass = require('./myClass');

Hope this helps

like image 119
Barnstokkr Avatar answered Oct 24 '22 16:10

Barnstokkr


I fixed this by making an entry point file like.

// index.js
require = require('esm')(module)
module.exports = require('./app.js')

and any file I imported inside app.js and beyond worked with imports/exports now you just run it like node index.js

Note: if app.js uses export default, this becomes require('./app.js').default when using the entry point file.

like image 24
Alex Cory Avatar answered Oct 24 '22 16:10

Alex Cory