Description
Describe the bug
new Error().stack[0].getFileName()
does not return the file name but undefined
. It should return the location of the file in the stack trace.
This is a common strategy to detect a caller file location. It is used in get-caller-file package, which has 25 million downloads per week accordingly to NPM. As it only requires a few lines of code it is very likely that the same technique is used even more.
It requires overloading of Error.prepareStackTrace
. The implementation in get-caller-file
package could be found here: https://github.com/stefanpenner/get-caller-file/blob/master/index.ts
I noticed this when trying to run ember-cli
, which relies on get-caller-file
.
Link to the blitz that caused the error
https://stackblitz.com/edit/node-ynwi5b?file=index.js
The following code returns undefined
in Stackblitz but a file path in regular node installation.
Error.prepareStackTrace = (_, stack) => stack;
const error = Error('myError');
console.log(error.stack[0].getFileName());
You can execute by running node index.js
in the blitz linked above.
As an alternative reproduction you could install get-caller-file
and follow the usage example given in its documentation. It will demonstrate the same difference between regular node installation and stackblitz.
Expected behavior
It should return a file path in Stackblitz as well.
Desktop (please complete the following information):
Browser name = Chrome
Full version = 90.0.4430.212
Major version = 90
navigator.appName = Netscape
navigator.userAgent = Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36
performance.memory = {
totalJSHeapSize: 70345978,
usedJSHeapSize: 67259162,
jsHeapSizeLimit: 4294705152
}
Additional context
node-bindings
package was facing the same issue: TooTallNate/node-bindings#29 It was fixed by this commit: TooTallNate/node-bindings@d0bd6f8