So I just started learning JavaScript. I am using plain JavaScript, and following the course on Code academy. Coming from C# I understood modules something like namespaces in C#. I am having trouble exporting, as well as importing them:
let Module = {
name: 'Hello'
};
export default Module;
This throws me an error - 'Uncaught SyntaxError: Unexpected token export'
I also tried the ES5 synthax:
let Module = {
name: 'Hello'
};
module.exports = Module;
This throws me another error - 'Uncaught ReferenceError: module is not defined'. I am using the newest version of Chrome, and I do not why JavaScript does not recognise these commands. It is very frustrating and cannot wrap my head around it. Therefore, I would like to ask someone to please help me with this problem.
If you're using the newest version of Chrome, it does support ES2015+ modules. You need to ensure your script
tag says it's a module:
<script type="module">
import name from './your-module-file.js';
console.log(name); // {name: "Hello"}
</script>
Then your first example will work.
But note that Module
is not a module. It's an object you're exporting from a module.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.