Skip to content
This repository has been archived by the owner on Sep 2, 2023. It is now read-only.

Use consistent error codes for MODULE_NOT_FOUND #360

Open
jkrems opened this issue Jul 24, 2019 · 9 comments
Open

Use consistent error codes for MODULE_NOT_FOUND #360

jkrems opened this issue Jul 24, 2019 · 9 comments

Comments

@jkrems
Copy link
Contributor

jkrems commented Jul 24, 2019

Right now ESM uses code: 'ERR_MODULE_NOT_FOUND' while CJS throws a similar error with code: 'MODULE_NOT_FOUND'. So far that looks to be accidental and worth fixing so code that catches these kinds of mistakes doesn't have to worry about which loader happened to throw it.

@guybedford
Copy link
Contributor

guybedford commented Jul 24, 2019

Great point, agreed this should be MODULE_NOT_FOUND for both.

Update - see below.

@guybedford
Copy link
Contributor

ERR_MODULE_NOT_FOUND was actually an intentional upgrade path in the ESM resolver since all Node.js error codes start with ERR_ these days.

If we want to change it to MODULE_NOT_FOUND though I'm fine with that too.

@jkrems
Copy link
Contributor Author

jkrems commented Jul 31, 2019

I think for the same reason that we do nodejs/node#28905, we should have one MODULE_NOT_FOUND error code for ESM and CJS. EDIT: "same reason" meaning "so people don't have to test X different codes when handling that a dependency can't be loaded because it doesn't exist (or isn't visible)".

@MylesBorins
Copy link
Contributor

Is there more work to do here?

@guybedford
Copy link
Contributor

The status is still:

  • CJS resolver throws MODULE_NOT_FOUND
  • ESM resolver throws ERR_MODULE_NOT_FOUND
  • configuration errors throw ERR_INVALID_PACKAGE_CONFIG for both

As a user, you will either be calling the CJS require.resolve and have to watch for MODULE_NOT_FOUND and ERR_INVALID_PACKAGE_CONFIG, or you will be writing a loader and calling the ESM default resolve and have to watch for ERR_MODULE_NOT_FOUND or ERR_INVALID_PACKAGE_CONFIG.

If someone feels strongly they want the new resolver to throw the same MODULE_NOT_FOUND as the old one without the ERR_ prefix now would be the time to post a PR.

Personally I do prefer the upgrade to ERR_MODULE_NOT_FOUND as an error standardization.

@jkrems
Copy link
Contributor Author

jkrems commented Nov 17, 2019 via email

@ljharb
Copy link
Member

ljharb commented Nov 18, 2019

It’ll affect everyone using resolve, and probably other resolvers as well.

@jkrems
Copy link
Contributor Author

jkrems commented Nov 18, 2019

It shouldn't affect anybody using require.resolve because it shouldn't ever encounter the import-loader ERR_ codes. The only scenario where the streams cross would be somebody writing the following:

import('./foo.js').catch(e => {
  if (e.code === 'ERR_MODULE_NOT_FOUND') {
    // triggers if ./foo.js isn't there or if foo.js is using `import` to load other files
    // and one of them isn't there.
  }
  if (e.code === 'MODULE_NOT_FOUND') {
    // triggers if ./foo.js is there but something in the dependency graph
    // is using `require` to load other files and one of them isn't there.
  }
});

The reason why I'd say "not an issue" is because most of the use cases for checking the errors that I'm aware of aren't interested in indirect dependencies. I would expect a guard in the code snippet above saying "but only if this error is about foo itself". Because indirect dependencies missing files is almost always a bug, not some optional feature.

@ljharb
Copy link
Member

ljharb commented Nov 18, 2019

I meant https://npmjs.com/resolve, which does look at those error codes.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants