Is "Learn JavaScript: Modules" part of pure JS?


#1

I’m having a hard time understanding what is part of what with the amount of frameworks and things that exist for Javascript.

I was just learning Node.js the other day and it does have export/import and require(). Yesterday I came across these terms in this Javascript course and I was thinking if they actually existed in pure javascript.

Say I have a file named a.js and a file named b.js. If on a.js I have var number = 5; I can simply do console.log(number) in b.js, if I’m using it on a web browser, with the scripts included in HTML.

And when I type export I get an error saying it doesn’t exist. So why is it taught in a Javascript course and there’s no mention about it not being default? Or am I wrong in thinking this?


#2

Did you import in b.js?

Export and import is default:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export

But its important to understand a little bit of history about JS:

https://www.infoworld.com/article/3210589/node-js/what-is-nodejs-javascript-runtime-explained.html

and how nodeJS was born


#3

If I open a main.html file with

<script src="a.js"></script>
<script src="b.js"></script>

And have

export let a = 5;

I get Uncaught SyntaxError: Unexpected token export on google chrome’s console.

Still with that configuration, if I define a var in a.js and log it in b.js it works.


#4

export is available for modules, as such, you have to tell the html file that a.js is a module, see here:

https://stackoverflow.com/questions/35336457/es2015-module-import-and-export-syntax-error


#5

Thank you! That makes sense.