ES modules in Node.js: The status quo

Share
  • June 24, 2019

The new ECMAScript (ES) modules are not fully compatible with previous language versions, so the JavaScript engine used needs to know for each file whether it is “old” JavaScript code or a “new” module.

For example, the strict mode preferred by many programmers introduced in ECMAScript 5 was once optional and had to be explicitly enabled, while it is always active in ES modules. Thus, the following snippet can be syntactically interpreted as both conventional JavaScript code and as an ES module:

a = 5;

As a classical Node.js module, this is equivalent to global.a = 5, because the variable a was not declared and strict mode was not explicitly activated, therefore a is treated as a global variable. If you try to load the same file as an ES module, you get the error “ReferenceError: a is not defined” because undeclared variables may not be used in strict mode.

    iJS React Cheat Sheet

    Free: React Cheat Sheet

    You want to improve your knowledge in React or just need some kind of memory aid? We have the right thing for you: the iJS React Cheat Sheet (written by Joel Lord). Now you will always know how to React!

Browsers solve the problem of distinction with an extension of the 

You may also like...