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");
}
}
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.
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.
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.
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.
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:
"type":"module"
in your project package.json
ts-node
or ts-node-dev
npm packages (for instant transpilation at development time) and write TypeScript in .ts
filesesbuild
package on npm) configured to transpile your ES6 javascript to a CommonJS target supported by your environment. (babel is no longer recommended)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 );
My two cents
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');
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With