简体   繁体   中英

How to Hide Internal Stack Trace Error in Node.js By Default?

I only want to display the TypeError line below, but hide all the internal details (the following 7 lines all starting with at).

 ~/Desktop/Practice> node "c:\Users\User\Practice\PracJS.js"
c:\Users\User\Practice\PracJS.js:6
let b = duck.prototype.isPrototypeOf(Bird)
                       ^
TypeError: Cannot read property 'isPrototypeOf' of undefined
    at Object.<anonymous> (c:\Users\User\Practice\PracJS.js:6:24)
    at Module._compile (internal/modules/cjs/loader.js:955:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
    at Module.load (internal/modules/cjs/loader.js:811:32)
    at Function.Module._load (internal/modules/cjs/loader.js:723:14)        
    at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)     
    at internal/main/run_main_module.js:17:11

I know I can do this with Error.stackTraceLimit = 0 by including it in the file. But I want to do this by default.

Is there any way to point Node a config file (setting Error.stackTraceLimit = 0 inside) so that whenever I run node PracJS.js (inside VSCode) so that it automatically applies Error.stackTraceLimit = 0 ?

Any help is appreciated! Thank you!

You could use --stack-trace-limit flag while running the javascript file. so in your case

node --stack-trace-limit=0 "c:\Users\User\Practice\PracJS.js"

OR

export NODE_OPTIONS='--stack-trace-limit=0'

It will be also worth mentioning to use NODE_ENV for these kind of cases. Some of the good packages (Express etc) uses this variable to control these things. so basically set NODE_ENV=production and you could also use this to control stack trace in your code.

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