Getting "on error (native)" instead of the actual node.js stacktrace, despite using err.stack

Using the following code example 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 tracing, although I ask err.stack.

Wouldn't that be the actual stack trace?

EDIT:

Here is a tiny piece of code that demonstrates my last (third) comment on 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 result that I get, showing that I did not even get the file location, where the error finally occurs when I look at the original error object, but get one in a 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
+4
source share
1 answer

() . , longjohn, , - , .

+3

All Articles