Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.Sign up
lib: improve error message on missing module #25690
The error message we provide when a module is missing is not very useful, specially for new users. Consider:
This has a lot internal details and very little useful information for the user. The key information relevant to the user in the above is the line
This PR proposes to change this to something like:
I'm somewhat on the fence about the new lines
We have been telling users to not rely overly on the precise string, but rather the error codes, but I'm not sure if the ecosystem has absorbed that fully. If not, this would be considered a semver-major.
I really like the idea, both the more descriptive message and the require stack.
A couple thoughts:
I really like the idea behind this. I am fine with the representation as is even though I would also like a property on the error (ideally an array) to have programmatic access to the require chain.
All Node.js internal logging would print that property fully (console, util.inspect, repl), so there's not a clear need to have this in the error message but the error message is likely just more accessible to the users and clearer to understand (while I personally have a slight preference for the following representation in the message).
$ node index.js internal/modules/cjs/loader.js:621 throw err; ^ Error: Cannot find module 'this-module-does-not-exist' Require chain: - /Users/ofrobots/tmp/require-error/who-loaded-dis.js - /Users/ofrobots/tmp/require-error/deeply-nested-module.js - /Users/ofrobots/tmp/require-error/direct-dependency.js - /Users/ofrobots/tmp/require-error/index.js at Function.Module._resolveFilename (internal/modules/cjs/loader.js:618:15) at Function.Module._load (internal/modules/cjs/loader.js:541:25) at Module.require (internal/modules/cjs/loader.js:673:17) at require (internal/modules/cjs/helpers.js:20:18) at Object.<anonymous> (/Users/ofrobots/tmp/require-error/who-loaded-dis.js:1:63) ...
Alright, I have revised this.
First up, I've added a structured property for the
As for the error message I have chosen to go with an expanded, multi-line, error message using the format suggested by @BridgeAR (thanks!). Rationale:
This is semver-major. I think that is fine.
There was additional suggestion of including a line-number. I think it is a reasonable idea, but perhaps as a follow-on?
CI is green: https://ci.nodejs.org/job/node-test-pull-request/20783/.