Using the .mjs
file extension for JavaScript files indicates that the file is an ES module. When using this convention, i don’t necessarily need to declare the package as a module with "type"="module"
in the package.json
file or use the module script tag. Instead, the file extension itself signals that the file should be treated as a module.
- Is it a good practice to use this convention instead of declearing the whole package as module? Because this provides a clear and explicit indication that this file is intended to be treated as a module.
- Will code behave exactly the same in both cases? Meaning that there is no single difference between using
.mjs
andtype=module
!?