2

我正在使用以下代码评估javascript捆绑的源代码nodejs

const javascriptCode = "..."
const javascriptSourceMap = "..."
const wrapper = NativeModule.wrap(javascriptCode);
const script = new vm.Script(wrapper, {
    displayErrors: true
});
script.runInNewContext();

当源代码中有错误时,我会收到这样的错误,因为没有提供源映射

ReferenceError: a is not defined
    at VueComponent.testSomething (evalmachine.<anonymous>:43112:7)
    at VueComponent.testSomethingAgain (evalmachine.<anonymous>:43109:12

如何为 VM 提供源映射,以便我可以获得正确的文件名而不是evalmachine.<anonymous>

提前致谢

4

1 回答 1

1

尝试

require('vm').runInThisContext('console.log(2);\ninvalidJsHere ?= 1;', 'foobar.js');

然后你得到

foobar.js:2
invalidJsHere ?= 1;
               ^

SyntaxError: Unexpected token =
    at new Script (vm.js:79:7)
    at createScript (vm.js:251:10)
于 2021-04-02T08:15:21.017 回答