eslint-community / eslint-plugin-n

Additional ESLint rules for Node.js
MIT License
222 stars 40 forks source link

Bug: Error "Can't resolve './DIR' in 'PATH'. Rule: `eslint: n/no-missing-import`" #302

Closed dalisoft closed 2 months ago

dalisoft commented 3 months ago

Environment

Node version: v20.14.0 npm version: v10.7.0 ESLint version: v9.5.0 eslint-plugin-n version: v17.9.0 Operating System: macOS 14.5

What rule do you want to report?

n/no-missing-import

Link to Minimal Reproducible Example

https://github.com/nanoexpress/ultimate-oss/tree/refactor

What did you expect to happen?

Auto-choose /index without warning but results are below

image

Participation

Additional comments

No response

scagood commented 3 months ago

This is because, as it's an ESM import, we dont allow index.* main files. Imports require a full file path.

I assume you're putting your code through a transpiler that is incorrectly resolving this :thinking:

dalisoft commented 3 months ago

If I’m add /index suffix, it works but without /index suffix it does not working. It’s TypeScript codebase and you can see via link to repository

scagood commented 3 months ago

Yeah, so in this case, the import is technically missing. This is because you should have the full file url in the import (this includes the file extension).

This I believe is to match node to browsers more closely, as exact file paths are required in a browser environment.


The import style you're attempting to use in the legacy require pattern with the esm import.

You can see us patching require here (where we allow it to lookup files via the main field in the package json, and searching for index when a directory is provided): https://github.com/eslint-community/eslint-plugin-n/blob/1bcb006e6d7ab4949a653d663801ee4dd9b0dc35/lib/util/import-target.js#L279-L286


We do attempt to compensate for typescript extensions here: https://github.com/eslint-community/eslint-plugin-n/blob/1bcb006e6d7ab4949a653d663801ee4dd9b0dc35/lib/util/import-target.js#L291-L294

Taking the mapping from here: https://github.com/eslint-community/eslint-plugin-n/blob/1bcb006e6d7ab4949a653d663801ee4dd9b0dc35/lib/util/get-typescript-extension-map.js#L21-L27

aladdin-add commented 3 months ago

https://dev.to/ayc0/typescript-50-new-mode-bundler-esm-1jic

I was wondering you can safely disable the rule if using ts + bundler - ts will report an error if you are importing a non-existent module. 🤔

dalisoft commented 2 months ago

If it's ok then i close this issue. Code with /index.ts works so no issue just looks a much different