I can't figure this out. I have a small app setup with an index.html that includes a javascript file. On the same directory as that file is another file named myJsModule.js with the following code:
export default class{
doStuff()
{
console.log("calling goStuff() from external Module");
}
}
The main javascript file that is loaded from the html then does the import in this way:
import myJsModule from "myJsModule";
// TESTING MODULES
let myNewModule = new myJsModule();
myNewModule.doStuff();
I have a local web server running using Node, so I'm accesing this index.hmtl through my localhost: http://127.0.0.1:8080.
Im getting the following error: Uncaught SyntaxError: Unexpected identifier (referring to myJsModule on my main js file). I also tried using babel to transpile this into previous javascript. I had the same problem using the "require".
Shouldn't my local server figure this out? Or am I using this wrong?
require
natively to import a CommonJS style module. ES6 specified a new import/export syntax, but browsers have been slow to implement modules with either syntax (I think maybe Safari is the exception). So in the mean time, best to bundle your browser code with Webpack or otherwise. Modules are really nice to work with, so it's well worth the extra build step.export function doStuff() { …}
, thenimport * as myJsModule from "myJsModule";
and callmyJsModule.doStuff();
.