image

Node JS or simply NODE is a backend javascript runtime environment, you can basically say a server-sided framework of javascript. ECMA script or ES6 is the syntax writing style for NODE and is better in many ways.

As of the latest version, Node.js supports both traditional CommonJs modules and the new Standard ECMAScript(ES) modules out of the box. Nodejs fully supports ECMAScript modules because these are currently specified and inter-operable between them and their original module format i.e CommonJs. Now you can use import and export syntax that is used for client-side JavaScript running in the browser. Import statements are allowed only in the ES module, but dynamic import() expressions are permitted in both commonJS and ESmodules.

 ES modules have asynchronous execution, so we use import() to load an ESmodule from a commonJS module but using require to load an ESmodule is not supported.

The ES modules in Node.js have javascript strict mode enabled by default, so you do not have to specify “use strict” at the beginning of every file.

Node.js treated javascript code as CommonJS modules by default. To let know Node.js that, you are using ES modules takes a little effort. Two most common ways to treat JavaScript code as ECMAScript modules are; by using the .mjs file extension and specifying “type”: “module” in the nearest package.json file or the  –input-type flag.

So we have two options,

1. Rename .js files to .mjs

2. Update the root package.json file, or add a package.json to the folder that contains ES modules and specify the type as a module. 

There is another possibility that is enabling the ES module in the root package.json file, and then renaming all CommonJS module Files to use the .cjs extension.

This addition personally for me is a good practice of coding using the new ES modules. Hope new inclusions like this come frequently in later stage node updates.

For more learning – Youtube -https://www.youtube.com/watch?v=WZQc7RUAg18

Blog – https://nodejs.org/api/esm.html