I have started working on an existing project based on Node.js. I was just trying to understand the flow of execution, where I encountered with some
*.mjs files. I have searched the web where I found that these are module based JS-files.
I want to know how is it different from
*.js files (how does it benefit)?
Node.js’s native module system is CommonJs.
It has experimental support for the standard ES6 module system and using a
.mjs file extension is part of one way to trigger that support. (As of Node 13, you can trigger support by setting
"type": "module", in package.json).
In short, it is used by Node.js to distinguish between CommonJS and ES6 module files.
- mjs an extension for EcmaScript modules
- An MJS file is a source code file containing an ES Module (ECMAScript Module) for use with a Node.js application.
- ES Modules allow web and application developers to organize code into smaller reusable components.
js added support for native ES modules, it controversially introduced the MJS file extension to differentiate the two and prevent applications from breaking.
NOTE: Some developers informally refer to MJS files as “Michael Jackson Script” files.
There are also performance benefits which gives you more reason to consider using it.