If you are running JavaScript on the command line using Node.js, and you want to load a file using the JavaScript language feature import, you must do one of two things:
- use the
.mjsextension for your JavaScript file to declare that it's a module, or - add
"type": "module"to yourpackage.json, which tells Node.js to consider all.jsfiles to be modules by default.
If you are running JavaScript on the command line using Node.js, and you want to load a file using the require() function that Node.js has supported for years and which is much more broadly compatible than the JavaScript language feature import, the file you load must not be a module. This is because TC39, the committee that designs new JavaScript features, chose violence.
This means that if you want to be able to load the same file either via import or via require(), you must either:
- use the
.cjsextension to declare that it's not a module, or - avoid adding
"type": "module"to yourpackage.json.
Okay, fine. Weird, but fine. But what if you want to load files from a web browser as well? Here's a thing about web browsers: they will not load JavaScript files unless they're served with the MIME type text/javascript. And most servers will only attach this MIME type to files with extension .js, not .cjs or .mjs. And browsers only support import, not require().
So.
If you want to make a file that's loadable via import and require(), on Node.js and the browser, it has to have extension .js, you cannot use "type": "module", and thus if your application contains files that use import you need to have two different names for each of those files, one with extension .js for the browser and one with extension .mjs for Node.js.
