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

module: refine module type mismatch error cases #35426

Closed
wants to merge 1 commit into from

Conversation

guybedford
Copy link
Contributor

This fixes up the error message in the case when importing a CJS module that contains ES module syntax.

Specifically, hiding the cjs-module-lexer parse failure message, and also then surfacing the warning context that informs the user about the "type" and ".mjs" format indicators to be able to correct the underlying issue.

Before this change:

import { name } from './module.cjs';

where module.cjs contains export / import syntax so is not valid CommonJS, would output as per the error in #35425.

With this change, we now get an error like:

(node:7249) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/home/guybedford/Projects/node/test/fixtures/es-modules/invalid-cjs.js:1
export var name = 5;
^^^^^^

SyntaxError: Unexpected token 'export'
Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines

@nodejs-github-bot nodejs-github-bot added the esm Issues and PRs related to the ECMAScript Modules implementation. label Sep 30, 2020
@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/modules

@guybedford
Copy link
Contributor Author

Ping @nodejs/modules-active-members for review. It could even be worth fast-tracking this.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm and +1 to fast-track

Copy link
Contributor

@MylesBorins MylesBorins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@MylesBorins MylesBorins added fast-track PRs that do not need to wait for 48 hours to land. request-ci Add this label to start a Jenkins CI on a PR. labels Sep 30, 2020
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 30, 2020
@nodejs-github-bot
Copy link
Collaborator

guybedford added a commit that referenced this pull request Oct 1, 2020
PR-URL: #35426
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
@guybedford
Copy link
Contributor Author

Landed in 726143e.

@guybedford guybedford closed this Oct 1, 2020
guybedford added a commit to guybedford/node that referenced this pull request Oct 1, 2020
PR-URL: nodejs#35426
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
codebytere pushed a commit that referenced this pull request Oct 4, 2020
PR-URL: #35426
Backport-PR-URL: #35405
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
@codebytere codebytere mentioned this pull request Oct 4, 2020
danielleadams pushed a commit that referenced this pull request Oct 6, 2020
PR-URL: #35426
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
@danielleadams danielleadams mentioned this pull request Oct 6, 2020
codebytere added a commit to electron/electron that referenced this pull request Oct 8, 2020
aduh95 pushed a commit to aduh95/node that referenced this pull request Oct 23, 2020
PR-URL: nodejs#35426
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
MylesBorins pushed a commit that referenced this pull request Nov 3, 2020
Backport-PR-URL: #35757
PR-URL: #35426
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
@MylesBorins MylesBorins mentioned this pull request Nov 3, 2020
MylesBorins pushed a commit that referenced this pull request Nov 16, 2020
Backport-PR-URL: #35757
PR-URL: #35426
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
joesepi pushed a commit to joesepi/node that referenced this pull request Jan 8, 2021
PR-URL: nodejs#35426
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
esm Issues and PRs related to the ECMAScript Modules implementation. fast-track PRs that do not need to wait for 48 hours to land.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants