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 import errors follow different logic than importing #51216

Closed
walkingeyerobot opened this issue Dec 19, 2023 · 3 comments · Fixed by #51223
Closed

module import errors follow different logic than importing #51216

walkingeyerobot opened this issue Dec 19, 2023 · 3 comments · Fixed by #51223

Comments

@walkingeyerobot
Copy link

Version

v21.4.0

Platform

linux

Subsystem

No response

What steps will reproduce the bug?

File main.mjs

import { four } from 'foo.mjs';

console.log(four());

File node_modules/foo.mjs:

export function four() {
  return 4;
}

Result:

node:internal/process/esm_loader:34
      internalBinding('errors').triggerUncaughtException(
                                ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'foo.mjs' imported from /usr/local/google/home/walkingeyerobot/nodetest/main.mjs
Did you mean to import foo.mjs?
    at packageResolve (node:internal/modules/esm/resolve:853:9)
    at moduleResolve (node:internal/modules/esm/resolve:910:20)
    at defaultResolve (node:internal/modules/esm/resolve:1130:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:396:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:365:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:240:38)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:85:39)
    at link (node:internal/modules/esm/module_job:84:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}

Node.js v21.4.0

How often does it reproduce? Is there a required condition?

No response

What is the expected behavior? Why is that the expected behavior?

I would expect one of:

  1. (preferred) The module loads, and the number 4 is printed
  2. The error message does not suggest to me to do exactly what I'm already doing

What do you see instead?

The error message tries to be helpful and suggests I try to import from foo.mjs, but that's exactly what I'm already doing. It seems like node can see there is a foo.mjs to import when printing an error message but not when doing the actual import. The error messaging and the importing should follow the same logic so as to give helpful error messages.

Additional information

No response

@Stebinraj
Copy link

import { four } from "./foo.mjs";

console.log(four());

The imported path is wrong.

@walkingeyerobot
Copy link
Author

import { four } from "./foo.mjs";

This doesn't work; foo.mjs is inside the node_modules folder

The imported path is wrong.

If that is the case, the error message should reflect that. Instead, the error message suggests that I import foo.mjs when I'm already importing foo.mjs.

If the code is wrong, then the error message suggestion is wrong as well. Error messages shouldn't suggest bad code. Hence the bug.

@ionicmc-js
Copy link

Hello im a new contributor! ill get to finding the source of the bug and fix it :)

nodejs-github-bot pushed a commit that referenced this issue Dec 23, 2023
PR-URL: #51223
Fixes: #51216
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
RafaelGSS pushed a commit that referenced this issue Jan 2, 2024
PR-URL: #51223
Fixes: #51216
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
richardlau pushed a commit that referenced this issue Mar 25, 2024
PR-URL: #51223
Fixes: #51216
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants