DEV Community

Discussion on: JavaScript Module Cheatsheet πŸ“„β€¬

Collapse
 
kevinhch profile image
Kevin

Hi, this only works in node or something like that? When I tried to reproduce the first example in Vanilla JS, always say the same error: SyntaxError: Cannot use import statement outside a module

Collapse
 
samanthaming profile image
Samantha Ming

To add to it ...the goal of import/export is to allow you to split your JS into separate files. To bring it back together, you need a module bundler (ie. Webpack or gulp) to join all the files together. The end result is one giant JS file. And that's the file you can use in your HTML.

In non dev terms, think of it as a kitchen. It's split into different workstations (why? because everyone can focus on what they're good it and is more efficient). And then there's the chef that puts everything together (think Gordon Ramsay lol). That chef is the module bundler. And she/he makes sure the dish is all combined so the customer can eat it. Hope this makes sense πŸ˜„

Collapse
 
brianwfl88 profile image
brianwfl88

ES6 import module only works in transpiler like webpack. The support for node is currently in experimental stage.

Collapse
 
samanthaming profile image
Samantha Ming

Yup you got it! Thanks for chiming and helping with the answer @brianwfl88 πŸ‘

Collapse
 
jefferyhus profile image
El Housseine Jaafari

You meant to say 'like babel', because webpack is a bundler.

Thread Thread
 
samanthaming profile image
Samantha Ming

In this instance because the files are all in modules, you will need a bundler like webpack or even gulp to join them all together πŸ™‚

i explain it here a bit more > dev.to/samanthaming/comment/i48b

Thread Thread
 
jefferyhus profile image
El Housseine Jaafari

Thanks, but I just corrected what he said πŸ˜….

Thread Thread
 
samanthaming profile image
Samantha Ming

Ah got it! I’ll also adjust my notes, thanks for the clarification πŸ™‚