Mörre Mörre - 24 days ago 14
Node.js Question

Getting "at Error (native)" instead of actual node.js stacktrace despite using err.stack

Using the following example code in io.js 3.2.0 64bit on Windows 10 and calling the following code with

node example.js


'use strict';

const fs = require('fs');

fs.readdir('I_DONT_EXIST', function (/**Error*/ err, /**string[]*/ files) {
if (err) {
console.log(err.stack);
}
});


I get

{ [Error: ENOENT: no such file or directory, scandir '...\I_DONT_EXIST']
errno: -4058,
code: 'ENOENT',
syscall: 'scandir',
path: '...\\I_DONT_EXIST' }
Error: ENOENT: no such file or directory, scandir '...\I_DONT_EXIST'
at Error (native)


So I get
at Error (native)
instead of the actual error trace even though I ask for
err.stack
.

Shouldn't this be the actual stack trace?

EDIT:

Here is a tiny piece of code that demonstrates my last (3rd) comment to the answer below.

'use strict';

const fs = require('fs');

fs.readdir('I_DONT_EXIST', function (/**Error*/ err, /**string[]*/ files) {
if (err) {
console.log('\n== 1) Original error');
console.log(JSON.stringify(err, Reflect.ownKeys(err), 4));
console.log('\n== 2) Original error "stack" property');
console.log(err.stack);

const e = new Error(err);
// Copy parameters I'm interested in from the original object
e.code = err.code;

console.log('\n\n== 3) New error');
console.log(JSON.stringify(e, Reflect.ownKeys(e), 4));
console.log('\n== 4) New error "stack" property');
console.log(e.stack);

console.log('\n\n== 5) Throw the error');
throw e;
}
});


The output I get, showing that I don't even get a file location of where the error finally occurs when I examine the original error object but get one in the new one, is:

== 1) Original error
{
"stack": "Error: ENOENT: no such file or directory, scandir 'C:\\Users\\xxx\\I_DONT_EXIST'",
"message": "ENOENT: no such file or directory, scandir 'C:\\Users\\xxx\\I_DONT_EXIST'",
"errno": -4058,
"code": "ENOENT",
"syscall": "scandir",
"path": "C:\\Users\\xxx\\I_DONT_EXIST"
}

== 2) Original error "stack" property
Error: ENOENT: no such file or directory, scandir 'C:\Users\xxx\I_DONT_EXIST'


== 3) New error
{
"stack": "Error: Error: ENOENT: no such file or directory, scandir 'C:\\Users\\xxx\\I_DONT_EXIST'\n at C:\\Users\\xxx\\test.js:11:19",
"message": "Error: ENOENT: no such file or directory, scandir 'C:\\Users\\xxx\\I_DONT_EXIST'",
"code": "ENOENT"
}

== 4) New error "stack" property
Error: Error: ENOENT: no such file or directory, scandir 'C:\Users\xxx\I_DONT_EXIST'
at C:\Users\xxx\test.js:11:19


== 5) Throw the error
C:\Users\xxx\test.js:20
throw e;
^

Error: Error: ENOENT: no such file or directory, scandir 'C:\Users\xxx\I_DONT_EXIST'
at C:\Users\xxx\test.js:11:19

Answer

Typically for async methods there won't be much of a (useful) stack trace available. There are modules like longjohn that can help provide more of a stack trace for such methods, but you wouldn't want to use that in production because of the overhead it incurs.

Comments