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

Friendlier error when the .ts file is not found #1357

Closed
dandv opened this issue Jun 1, 2021 · 5 comments · Fixed by #1373
Closed

Friendlier error when the .ts file is not found #1357

dandv opened this issue Jun 1, 2021 · 5 comments · Fixed by #1373
Labels
you can do this Good candidate for a pull request.
Milestone

Comments

@dandv
Copy link

dandv commented Jun 1, 2021

I mistyped the name of my script, and node spat out an ugly-looking error instead of a simple "Hey dude, file not found":

$ node --loader=ts-node/esm myscrypt.ts
(node:27) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)

/home/dandv/prg/tstest/node_modules/ts-node/dist-raw/node-esm-resolve-implementation.js:380
    throw new ERR_MODULE_NOT_FOUND(
          ^
Error: ERR_MODULE_NOT_FOUND /home/dandv/prg/tstest/myscrypt.ts /home/dandv/prg/tstest/ module
    at finalizeResolution (/home/dandv/prg/tstest/node_modules/ts-node/dist-raw/node-esm-resolve-implementation.js:380:11)
    at moduleResolve (/home/dandv/prg/tstest/node_modules/ts-node/dist-raw/node-esm-resolve-implementation.js:815:10)
    at Object.defaultResolve (/home/dandv/prg/tstest/node_modules/ts-node/dist-raw/node-esm-resolve-implementation.js:926:11)
    at /home/dandv/prg/tstest/node_modules/ts-node/src/esm.ts:67:38
    at Generator.next (<anonymous>)
    at /home/dandv/prg/tstest/node_modules/ts-node/dist/esm.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (/home/dandv/prg/tstest/node_modules/ts-node/dist/esm.js:4:12)
    at resolve (/home/dandv/prg/tstest/node_modules/ts-node/dist/esm.js:31:16)
    at Loader.resolve (node:internal/modules/esm/loader:89:40)

Would it be possible to simplify this error, or is this completely up to Node?

@cspotcode
Copy link
Collaborator

What error do you get when you do this without ts-node? myscript.js, not using the ts-node loader, not writing typescript, what is the error that vanilla node gives in this situation? In general, we try to match node.

@dandv
Copy link
Author

dandv commented Jun 2, 2021

Node spits out a somewhat shorter message,

$ node notfound.js
node:internal/modules/cjs/loader:944
  throw err;
  ^

Error: Cannot find module '/home/dandv/notfound.js'
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:941:15)
    at Function.Module._load (node:internal/modules/cjs/loader:774:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

@cspotcode
Copy link
Collaborator

Ok, we shouldn't shorten the call stack because that would be violating some fundamental expectations for errors. There's a pull request where I attempted a prototype of this, but it's a messy hack.

We can improve the error message, though. Should be a straightforward PR for you or anyone else you can convince to do it. :) Below are detailed notes to explain what needs to be done, but again, it's a pretty simple improvement. Don't let the verbosity scare you.


Our ESM resolver is copy-pasted straight out of node's own source code. This is the resolver, and it throws ERR_MODULE_NOT_FOUND in various places. https://github.com/TypeStrong/ts-node/blob/main/dist-raw/node-esm-resolve-implementation.js

The errors, however, are stub implementations: https://github.com/TypeStrong/ts-node/blob/main/dist-raw/node-errors.js
Just enough to set a code but not enough to set nice error messages.

So if you or anyone else wants to implement nicer errors, I believe all you need to do is update node-errors.js with a nicer implementation.

In our copy-pasted resolver, we've deliberately left node's original require calls commented out so you know where to look in node's source to find the implementations.
https://github.com/TypeStrong/ts-node/blob/main/dist-raw/node-esm-resolve-implementation.js#L76-L89

In this case, the errors were imported from internal/errors which is this file: https://github.com/nodejs/node/blob/master/lib/internal/errors.js

@cspotcode cspotcode added the you can do this Good candidate for a pull request. label Jun 2, 2021
@cspotcode
Copy link
Collaborator

I've labelled this "help wanted" to indicate we will await and accept a PR improving our error messages. If you want other people to more easily find this issue, feel free to rename it to something more generic like "improve error messages" or something like that.

@tars0x9752
Copy link
Contributor

I'd like to work on this. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
you can do this Good candidate for a pull request.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants