30
votes

I'm trying to use classes in pure JavaScript, so I'm facing the error "Uncaught SyntaxError: Cannot use import statement outside a module" and can't solve it.

File1.js - Main file

import example from "./file2";

var test = new example();

File2.js - Class file

export default class example {
    constructor() {
        console.log("hello world");
    }
}
1
Browsers cannot process import so you would need to use something like Babel, short explanation here: stackoverflow.com/a/41662216/779600 - nihiser
This question seems to be about using import statements outside of the browser, so it's different from the linked questions. - Ola Eldøy

1 Answers

35
votes

Add files with type="module":

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