Node.js · Junior · Question 14 of 75
How does Node.js decide that a .js file is an ES module?
- ✓Correct answer: The nearest
package.jsonhas"type": "module"(or the file uses the.mjsextension) - 2Node.js always treats
.jsfiles as ES modules - 3The file name starts with
esm-
Explanation
Without "type": "module", .js files are CommonJS. .mjs is always ESM and .cjs is always CommonJS. Recent versions can also detect ESM syntax in ambiguous files.