Skip to content

A package.json lacking a "type" field is not treated as if it contained "type": "commonjs" #30245

@kimamula

Description

@kimamula
  • 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues with confirmed bugs.esmIssues and PRs related to the ECMAScript Modules implementation.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions