Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
kimamula opened this issue Nov 4, 2019 · 1 comment
Labels
confirmed-bug Issues with confirmed bugs. esm Issues and PRs related to the ECMAScript Modules implementation.

Comments

@kimamula
Copy link

kimamula commented Nov 4, 2019

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

@kimamula kimamula changed the title Top-level "type" field of package.json file does not default to "commonjs" A package.json lacking a "type" field is not treated as if it contained "type": "commonjs" Nov 4, 2019
@guybedford
Copy link
Contributor

Thanks for the really clear bug report here and for testing this edge case. You are exactly right about the expectations here. I've posted a fix for this in #30256.

@guybedford guybedford added confirmed-bug Issues with confirmed bugs. esm Issues and PRs related to the ECMAScript Modules implementation. labels Nov 5, 2019
@Trott Trott closed this as completed in 1eee0b8 Nov 8, 2019
MylesBorins pushed a commit that referenced this issue Nov 17, 2019
Fixes: #30245
PR-URL: #30256
Reviewed-By: Myles Borins <myles.borins@gmail.com>
Reviewed-By: Jan Krems <jan.krems@gmail.com>
targos pushed a commit that referenced this issue Dec 1, 2019
Fixes: #30245
PR-URL: #30256
Reviewed-By: Myles Borins <myles.borins@gmail.com>
Reviewed-By: Jan Krems <jan.krems@gmail.com>
MylesBorins pushed a commit that referenced this issue Dec 17, 2019
Fixes: #30245
PR-URL: #30256
Reviewed-By: Myles Borins <myles.borins@gmail.com>
Reviewed-By: Jan Krems <jan.krems@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug Issues with confirmed bugs. esm Issues and PRs related to the ECMAScript Modules implementation.
Projects
None yet
Development

No branches or pull requests

2 participants