Context
When using code from other files or dependencies, we need to import them into our files. There are two ways to do it: using CommonJS syntax or ES modules.
CommonJS
Standard in NodeJS
const module = require('module.js);
module.method();
ES modules
It needs files with .mjs
extension or type
to be module
in package.json with NodeJS >= 13.
import { doSomething } from "module";
doSomething();
Questions
- Why does NodeJS requires files to have
.mjs
extension to be imported using import declaration?