Under the hood the ES6 engine will load bar.js when it evaluates import { Bar } from 'javascripts/bar';, and block upon return of that module over HTTP? Or is bar.js downloaded prior to evaluation of the script in index.html?

There is no correct answer to this yet, because there is no specification for how modules are loaded. ES2015 only specifies the module syntax, but how runtimes interpret that is not yet standardized. For example, it's very unlikely any loader specification that is eventually implemented will allow you to omit the .js as you do. And it's also very unlikely that <script> tags will be allowed to use import.

However, setting aside any syntactic missteps you made, I can tell you in general terms what is likely to be standardized for the browser loader. It is the latter: imports are determined and downloaded ahead of time, before any script execution happens. (For the Node.js loader, however, it is likely to be blocking.)

Because bar.js is loaded using the import keyword, the globals in bar.js are scoped to that module and are not visible globally?

That depends on what you mean by globals. If you declare a global with e.g. window.x = 5, that will still mutate the global object. Modules do not get separate global objects to mess with.

However, if you mean "accidental" globals like those declared with var or function declarations at the top-level, the answer is that in modules top-level var and function declarations do not cause global properties to be defined.

(Note that in both modules and scripts, top-level let and const declarations do not create properties on the global object.)

Now if I want to concatenate modules, I will continue to need to wrap my modules in IIFEs, so that their scopes remain distinct (or at least use a build step that does this under the hood)?

If you want to concatenate modules, you're going to have a lot bigger problems than those you describe. For example, import and export are only usable at the top level, and not inside an IIFE. Modules are not meant to be concatenated, as doing so is actively harmful to performance given modern browsers and servers.

Answer from Domenic on Stack Overflow
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Modules
JavaScript modules - JavaScript | MDN
There is also a type of export called the default export — this is designed to make it easy to have a default function provided by a module, and also helps JavaScript modules to interoperate with existing CommonJS and AMD module systems (as explained nicely in ES6 In Depth: Modules by Jason ...
🌐
The Odin Project
theodinproject.com › lessons › javascript-es6-modules
ES6 Modules | The Odin Project
While the module pattern used to play a big part in helping us manage this, the release of ES6 (sometimes referred to as ES2015) gave us actual “modules” and thus they are often referred to as “ES6 modules” or “ESM”.
Discussions

Understanding ES6 modules
Now if I want to concatenate modules, ... wrap my modules in IIFEs, so that their scopes remain distinct (or at least use a build step that does this under the hood)? ... Great question. That looks right. I'd love to confirm, but I am not certain enough, especially about the last 2 sentences. +1'ing because it'd be great to get an answer that explains some edge cases. ... Save this answer. ... Show activity on this post. Under the hood the ES6 engine will ... More on stackoverflow.com
🌐 stackoverflow.com
ES6 - What are good side of ES6 modules?
Advantages of ES6 modules: Syntax is more compact for common tasks. Static structure (can’t conditionally import or export) enables a few important use cases: faster access to imports, smaller bundles (via tree-shaking – see the bundler Rollup ), better linting, etc. Automatic support for cyclic dependencies (you need to be careful in CommonJS) More on reddit.com
🌐 r/javascript
9
4
February 2, 2016
Is it better to use es6 modules with Node?
It’s basically a transition moment right now. The import/export syntax didn’t exist on the front-end when Node made the require syntax. Once import/export was added as a language feature, Node began to support it but not all existing modules have been updated. In fact, most probably haven’t as import/export is a relatively new feature. So, my advice would be to stick with require for the time being but plan to switch eventually. (I’m personally probably going to wait a year or so. When I do decide to switch, I’m going to test each module one-by-one and see which ones have or haven’t been updated. If it’s been that long without an update, it might be time to look for an alternative anyway.) More on reddit.com
🌐 r/node
28
39
April 9, 2021
${JavaScript} Are there any methods to use ES6 modules locally* without violating the CORS policy?
There's tons! These are the most common and easiest: You can simply run a localhost server with a simple tool like http-server . No internet connection required since it's all local to your machine. You can bundle your code with a bundler like Webpack, Rollup, etc which would then transform all of your files into one which you can easily use in any browser. More on reddit.com
🌐 r/learnprogramming
3
1
September 3, 2022
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › es6-modules
ES6 Modules - GeeksforGeeks
July 12, 2025 - ES6 modules enhance JavaScript by allowing developers to modularize code, breaking it into manageable pieces that can be imported and reused across different parts of an application.
🌐
W3Schools
w3schools.com › React › react_es6_modules.asp
React ES6 Modules
ES Modules rely on the import and export statements.
Top answer
1 of 1
5

Under the hood the ES6 engine will load bar.js when it evaluates import { Bar } from 'javascripts/bar';, and block upon return of that module over HTTP? Or is bar.js downloaded prior to evaluation of the script in index.html?

There is no correct answer to this yet, because there is no specification for how modules are loaded. ES2015 only specifies the module syntax, but how runtimes interpret that is not yet standardized. For example, it's very unlikely any loader specification that is eventually implemented will allow you to omit the .js as you do. And it's also very unlikely that <script> tags will be allowed to use import.

However, setting aside any syntactic missteps you made, I can tell you in general terms what is likely to be standardized for the browser loader. It is the latter: imports are determined and downloaded ahead of time, before any script execution happens. (For the Node.js loader, however, it is likely to be blocking.)

Because bar.js is loaded using the import keyword, the globals in bar.js are scoped to that module and are not visible globally?

That depends on what you mean by globals. If you declare a global with e.g. window.x = 5, that will still mutate the global object. Modules do not get separate global objects to mess with.

However, if you mean "accidental" globals like those declared with var or function declarations at the top-level, the answer is that in modules top-level var and function declarations do not cause global properties to be defined.

(Note that in both modules and scripts, top-level let and const declarations do not create properties on the global object.)

Now if I want to concatenate modules, I will continue to need to wrap my modules in IIFEs, so that their scopes remain distinct (or at least use a build step that does this under the hood)?

If you want to concatenate modules, you're going to have a lot bigger problems than those you describe. For example, import and export are only usable at the top level, and not inside an IIFE. Modules are not meant to be concatenated, as doing so is actively harmful to performance given modern browsers and servers.

🌐
freeCodeCamp
freecodecamp.org › news › how-to-use-es6-modules-and-why-theyre-important-a9b20b480773
A Practical guide to ES6 modules
November 27, 2018 - In this article, we’ll create a simple dashboard using ES6 modules, and then present optimization techniques for improving the folder structure and ease write less code.
Find elsewhere
🌐
Coherent Labs
coherent-labs.com › home › javascript modules
JavaScript Modules are part of our ES6 series tutorial from Coherent Labs
December 13, 2017 - Essentially, modules in ES6 are files that contain JavaScript code, that automatically runs in strict mode (and there is no way to opt-out). That code may contain declarations as variable declarations, function declarations, class declarations, etc.
🌐
Medium
medium.com › the-node-js-collection › an-update-on-es6-modules-in-node-js-42c958b890c
An Update on ES6 Modules in Node.js | by James M Snell | Node.js Collection | Medium
February 16, 2017 - As soon as the require() function returns in app.js , the shape of the foobar module is known and can be used. All of this happens within the course of the same Node.js event loop tick. Critical to understanding the difference between CommonJS and ES6 modules is the fact that the shape (the API) of a CommonJS module cannot be determined until after the code is evaluated — and even after evaluation, the shape can be mutated by other code at any time.
🌐
Web Dev Simplified
blog.webdevsimplified.com › 2021-11 › es6-modules
ES6 JavaScript Modules
ES6 modules are one of the best features added to JavaScript since it makes writing clean code exponentially easier.
🌐
Medium
david-gilbertson.medium.com › es6-modules-in-the-browser-are-they-ready-yet-715ca2c94d09
ES6 modules in the browser: are they ready yet? | by David Gilbertson | Medium
March 5, 2020 - When considering whether or not to use ES6 modules in the browser, it’s really a decision between using modules or a bundler like Webpack or Parcel or Rollup.
🌐
Microsoft Developer Blogs
devblogs.microsoft.com › dev blogs › typescript › announcing typescript 6.0
Announcing TypeScript 6.0 - TypeScript
March 23, 2026 - This is basically a field called imports which allows packages to create internal aliases for modules within their package.
🌐
DEV Community
dev.to › icncsx › es6-modules-in-node-57l7
ES6 Modules in Node - DEV Community
June 14, 2020 - CommonJS (Node) – the module system created for Node. ES6 Modules (Native) – the specification written by ECMA TC39.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › es6 tutorial › es6 modules
ES6 modules
April 15, 2023 - ES6 module consists of the file that contains the javascript code in it. It does not contain the module keyword or use strict keywords in it and can be read as a simple script file.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
NVIDIA
nvidia.com › en-us › autonomous-machines › embedded-systems › jetson-orin › nano-super-developer-kit
Jetson Orin Nano Super Developer Kit | NVIDIA
The NVIDIA Jetson Orin Nano™ Super Developer Kit is a compact, yet powerful computer that redefines generative AI for small edge devices
🌐
Fireship
fireship.dev › javascript-modules-iifes-commonjs-esmodules
JavaScript Modules: From IIFEs to CommonJS to ES6 Modules
In this post you'll learn all about JavaScript modules including the IIFE pattern, CommonJS, and ES6 Modules.
🌐
Reddit
reddit.com › r/javascript › es6 - what are good side of es6 modules?
r/javascript on Reddit: ES6 - What are good side of ES6 modules?
February 2, 2016 -

Hello!

All or most of us know, that in ES6 world, module is an entity that export certain things. It may export either a list of named things like constants, variables, functions, or export "default" which is unnamed thing.

So now, when we write modules, we can either export some single class, function or object as "default" - this makes sense when we are exporting classes or functions as complex as they are taking whole module.

But in typical webdev, many apps are split to infrastructure layer and service layer. We know that insfrastructure is an connect point to services, and services are business models. Following SOLID, we will probably end having some modules like email notification module, authorization module, pucharses module et cetera.

Now whats the correct design way of this in Node.js and ES6? My service layer modules are typically a series of functions that are doing similar things, like email notification module has a function to send register email to client, send pucharse notification as email to client, send reminder password to client.

Following ES6 module syntax, I did it by exporting certain functions, like:

service/email.js:

export function sendRegisterEmail( client ) {...}
export function sendPucharseNotificationEmail( client , pucharse ) {...}
export function sendPasswordReminderEmail( client ) {...}

Now whenever I'd want to use these service (lets skip event-based communication for now, it is just an example), I must load it using either:

import * as EmailService from 'service/mail.js';

or

import sendRegisterEmail from 'service/email.js';

I've tried both of them but noone fits me perfectly. Somebody said to me that first one is better because "in future ES6 or module loaders will only use functions you actually require". I bet this is barely possible due to overall nature of JS, as it is non deteministic at parse/compile time and it wouldnt really save much inside your application. If you have external module like lodash, importing only map and reduce from it could (if it would actually truncate dead code) save you some KB of unused code, but inside YOUR modules you will rarely have function that is not used at all, because what would be point of these? If I wouldn't need sendPucharseNotifcation in my email service, I'd just erase that function from source entirely.

There is also one thing more - it shades real source of function. In ~200+ LOC module, when you see a line "doSomething(xxx)" you must scroll source all way top in order to see that:

 import doSomething from 'service/some-domain/something-manager.js

First approach make much more sense especially if your team is following convention in all files, i.e. it is always:

import * as SomethingManager from 'service/some-domain/something-manager.js';

Then when you see in your code:

SomethingManager.doSomething(xxx) 

You already see whats going on here. It is just something like mm namespace? You know already source of function without needing to scroll into imports section of your code.

But this approach is quite tricky as well, because there WILL be some modules that actually work as exporting single entity, lets say model class, controller function that takes express app as param and registers its own routes there or middleware function. So you must require them using default syntax, that is:

import User from 'model/user.js';
import AuthController from 'controller/auth.js';
import AdminMiddleware from 'middleware/admin.js';

But mixing this with modules that export named functions started to make mess in my code. I was making A LOT mistakes due to incorrectly requiring certain things, like using:

import EmailService from 'service/email.js';

Which yields undefined as proper syntax in this case is: import * as EmailService from 'service/mail.js';

I am now further in developing this project and it made me a habit to open a module source file and check what and how it exports things before I will import it. It also looks bad when there are mixed styles of imports like:

import User from 'model/user.js';
import * as UserRepository from 'repository/users.js';
import AuthMiddleware from 'middleware/auth.js';

etc.

I know that I'd just do export default { ... } and put all my functions into exported object but I dont like repeating myself in code and it also looks more like a workaround than a real use case.

So what are real good sides of ES6 modules, because it has a lot drawbacks for me. I wanted to learn ES6 and I started to use ES6 modules but from what I see now it just make more mess than old commonjs require() thing.

I'd also love to listen about your guys point of view about managing modules in ES6 project. Thanks in advance!

Top answer
1 of 3
3
Advantages of ES6 modules: Syntax is more compact for common tasks. Static structure (can’t conditionally import or export) enables a few important use cases: faster access to imports, smaller bundles (via tree-shaking – see the bundler Rollup ), better linting, etc. Automatic support for cyclic dependencies (you need to be careful in CommonJS)
2 of 3
1
ES6 module syntax gives some flexibility; you do need to figure out how to use it that makes sense on your project. Also note: There is also one thing more - it shades real source of function. In ~200+ LOC module, when you see a line "doSomething(xxx)" you must scroll source all way top in order to see that: A good IDE (eg, anything by Jetbrains, but Sublime or whatever should do this too) will help a lot here in terms of providing insight into where functions are coming from, letting you jump straight to their definition, etc. I was making A LOT mistakes due to incorrectly requiring certain things, like using: Similarly, a good IDE will flag incorrect imports automatically, and will provide autocomplete so you don't mess them up. So what are real good sides of ES6 modules, because it has a lot drawbacks for me. I wanted to learn ES6 and I started to use ES6 modules but from what I see now it just make more mess than old commonjs require() thing. I think you're deeply overstating the difference between commonjs require and ES6 import; there should be a 1:1 mapping between your old require statements and your new import statements. Much of your post seems to be "I was structuring my modules like this, now I'm structuring them like that, and I hate the change." Okay, so don't change? I like using named exports, but they're not needed; you can just export a big object as your default export just like you were with commonjs. You're asking what the advantages are to the ES6 module syntax, but I'm actually wondering what the drawbacks are; I'm not really seeing any listed in your post.
🌐
Three.js
threejs.org › docs
three.js docs
three.js · docs manual · Animation · AnimationAction · AnimationClip · AnimationMixer · AnimationObjectGroup · AnimationUtils · BooleanKeyframeTrack · ColorKeyframeTrack
🌐
npm
npmjs.com › package › dotenv
dotenv - npm
Because ES6 imports are hoisted, put the dotenv import and config() call at the very top, before any other imports that rely on process.env.
      » npm install dotenv
    
Published   Apr 12, 2026
Version   17.4.2
🌐
Mozilla Hacks
hacks.mozilla.org › home › articles › es6 in depth: modules
ES6 In Depth: Modules – Mozilla Hacks - the Web developer blog
August 14, 2015 - The new standard is designed to interoperate with existing CommonJS and AMD modules. So suppose you have a Node project and you’ve done npm install lodash. Your ES6 code can import individual functions from Lodash: