- Version: 13.0.1
- Platform: macOS Mojave
- Subsystem:
ECMAScript modules Document says:
A package.json lacking a "type" field is treated as if it contained "type": "commonjs".
To confirm this, I have created folders and files as follows (file contents are written after colons).
root/
package.json : { "type": "module" }
sub1/
foo.js : const fs = require('fs');
package.json : {}
sub2/
foo.js : const fs = require('fs');
package.json : { "repository": { "type": "git" } }
When I execute $ node --experimental-modules ./sub1/foo.js in the root folder, it results in an error Error [ERR_REQUIRE_ESM]: Must use import to load ES Module, while $ node --experimental-modules ./sub2/foo.js does not throw any error.
Therefore, the document is incorrect and it is likely non-top-level "type" fields in package.json file affects whether the JS files are treated as ES modules or CommonJS modules.
ECMAScript modules Document says:
To confirm this, I have created folders and files as follows (file contents are written after colons).
When I execute
$ node --experimental-modules ./sub1/foo.jsin therootfolder, it results in an errorError [ERR_REQUIRE_ESM]: Must use import to load ES Module, while$ node --experimental-modules ./sub2/foo.jsdoes not throw any error.Therefore, the document is incorrect and it is likely non-top-level
"type"fields inpackage.jsonfile affects whether the JS files are treated as ES modules or CommonJS modules.