patriksimek / vm2

Advanced vm/sandbox for Node.js
MIT License
3.86k stars 293 forks source link

Module parse failed: 'return' outside of function #509

Closed madhuni closed 1 year ago

madhuni commented 1 year ago

I am facing issue while consuming the setup-sandbox.js file via webpack config. The application is failing for the below error:

    ERROR in ./node_modules/vm2/lib/setup-sandbox.js 453:0
    Module parse failed: 'return' outside of function (453:0)
    You may need an appropriate loader to handle this file type.
    | }
    | 
    > return {
    |   __proto__: null,
    |   readonly,

I am suspecting, the setup-sandbox.js file returns this object outside of a function which is causing the issue. As per the MDN docs, a return statement should always be inside the function. Wanted to understand how we can bypass this error? Is there any solution for the same in Webpack config?

return {
    __proto__: null,
    readonly,
    global
};
XmiliaH commented 1 year ago

The setup-sandbox.js and setup-node-sandbox.js files are loaded as strings and not through require. Furthermore, the bridge.js is loaded as string and via require. They should not be transformed as this could have security implications. It is possible to use plugins such as babel-plugin-static-fs to include the sources of the files where it would be loaded dynamically.