Question

Background: I developed a small module system for node.js that asynchronously loads all required code from a separate source server and compiles it with eval(code). My problem is that for all run-time errors in such "eval-ed" code, the stack trace shows <anonymous> as the file name --- I cannot distinguish which code piece is referred to.

My question: Is there a way to set the file-name for eval so that the stack trace shows some program-defined string instead of <anonymous>?

My current (imperfect) solution: As of now, I write the code to a file and load it with require(file). This will sow the file name --- but it is a complicated workaround.

Note: Normally, the run-time errors do not show up at the compilation of the eval call, but later, when the compiled code is used. Thus, try/catch around the eval does not help to identify which code piece failed.

Was it helpful?

Solution 2

Don't use eval (ever). For your purpose you can use vm module (vm.runInThisContext or else). You can pass file name as an argument. require internally does the same.

OTHER TIPS

Eventually, this new feature of the V8 engine will make its way to Node.js:

Issue 2948: Support //# sourceURL= and //# sourceMappingURL= in v8's parser

This method works for Browsers currently.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top