简体   繁体   中英

node.js: suppress require()'s echoing of parse errors to stderr

node.js 10.26 will rightfully throw an error when you try to require a file that is not valid javascript (or JSON).

My problem is that it also outputs to stderr:

borken.js - know to be broken javascript file

,,>Z>badfile!=-csa&asd;'asdl ds]=}{ADS}DAS:LMFSV'

test.js

try {
    var borken = require('./borken');
} catch (e) {} finally {
    console.log('finally!');
}

Expected

$ node test.js
finally!

Actual

$ node test.js
    /path/to/borken.js:1
(function (exports, require, module, __filename, __dirname) { asd;'asdl
                                                                  ^^^^^
finally!

The first three lines are output to stderr (running as node test.js 2> /dev/null suppresses them).

Is there any way to get rid of them?

What I've done so far: I debugged while following the source and narrowed down the culprit to:

lib/module.js:439

var compiledWrapper = runInThisContext(wrapper, filename, true); 

runInThisContext is defined in lib/vm.js , which unfortunately is a C++ wrapper, so I couldn't really follow what is going on through there. I guess I'm looking for a magic "don't spam stderr" flag or any knowledge of this being fixed in any latest versions.

Apparently this has been fixed in the dev branch (0.11.x) but not in the latest stable (0.10.29)

Github issue: Syntax errors are printed to stderr, even when wrapped with try/catch #6920

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM