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

esm: invalid extension error should provide parent context in error #30721

Closed
LJNeon opened this issue Nov 29, 2019 · 4 comments
Closed

esm: invalid extension error should provide parent context in error #30721

LJNeon opened this issue Nov 29, 2019 · 4 comments
Labels
esm Issues and PRs related to the ECMAScript Modules implementation. feature request Issues that request new features to be added to Node.js. good first issue Issues that are suitable for first-time contributors. help wanted Issues that need assistance from volunteers or PRs that need help to proceed.

Comments

@LJNeon
Copy link

LJNeon commented Nov 29, 2019

  • Version: v13.2.0
  • Platform: Windows 10, 64-bit
  • Subsystem: N/A

When trying to import a JSON file, the error message provided is the following:

(node:7004) ExperimentalWarning: The ESM module loader is experimental.
internal/modules/run_main.js:50
    internalBinding('errors').triggerUncaughtException(
                              ^

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension: C:\Users\Mercy\Desktop\New folder\node_modules\patron.js\package.json
    at Loader.resolve [as _resolve] (internal/modules/esm/default_resolve.js:114:13)
    at Loader.resolve (internal/modules/esm/loader.js:74:33)
    at Loader.getModuleJob (internal/modules/esm/loader.js:148:40)
    at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:41:40)
    at link (internal/modules/esm/module_job.js:40:36) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

How to reproduce:

  1. Create test.mjs with import './package.json'
  2. Run node test.mjs

To help debug this error we should ensure it provides the name of the parent module that attempted the file extension load - something like imported from /path/to/test.mjs.

@addaleax
Copy link
Member

@nodejs/modules-active-members

@addaleax addaleax added the esm Issues and PRs related to the ECMAScript Modules implementation. label Nov 29, 2019
@guybedford
Copy link
Contributor

If you run this example with node --experimental-json-modules index.js it gives a different error so the problem is that you actually have an import to the package.json file somewhere in the tree.

We should look at improving this error message to include the parent module that cause the load to provide debugging context. For that reason I'm changing the title and marking this as a bug.

@guybedford guybedford changed the title esm: modules with file extention in name cannot be imported esm: invalid extension error should provide parent context in error Nov 29, 2019
@guybedford guybedford added feature request Issues that request new features to be added to Node.js. help wanted Issues that need assistance from volunteers or PRs that need help to proceed. good first issue Issues that are suitable for first-time contributors. labels Nov 29, 2019
@guybedford
Copy link
Contributor

(@LJNeon I hope you don't mind me rewording your original issue to reflect the root cause, we can move it to another issue if you'd prefer too.)

@qualitymanifest
Copy link
Contributor

I'd like to give this a try.

Would it be okay to modify default_resolve (the only place I see this error being used) so that parentURL is passed into ERR_UNKNOWN_FILE_EXTENSION, or is there a better way of getting the parent URL inside the error function?

targos pushed a commit that referenced this issue Dec 9, 2019
- default_resolve updated to pass parentURL into error
- ERR_UNKNOWN_FILE_EXTENSION updated to include parentURL
- test added to check for import message in error

PR-URL: #30728
Fixes: #30721
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
MylesBorins pushed a commit that referenced this issue Jan 12, 2020
- default_resolve updated to pass parentURL into error
- ERR_UNKNOWN_FILE_EXTENSION updated to include parentURL
- test added to check for import message in error

PR-URL: #30728
Fixes: #30721
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
BethGriggs pushed a commit that referenced this issue Feb 6, 2020
- default_resolve updated to pass parentURL into error
- ERR_UNKNOWN_FILE_EXTENSION updated to include parentURL
- test added to check for import message in error

PR-URL: #30728
Fixes: #30721
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@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. feature request Issues that request new features to be added to Node.js. good first issue Issues that are suitable for first-time contributors. help wanted Issues that need assistance from volunteers or PRs that need help to proceed.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants