How to allow filesystem access

461 Views Asked by At

I want to provide filesystem access for use with fs. How can I grant the vm2 process access to a specific directory?

I've tried setting external to true and a root of /. The process is able to access the directory when run outside vm2.

1

There are 1 best solutions below

0
On

Did you set builtin: ['fs']?

Try the below code sample

const {NodeVM} = require('vm2');

const vm = new NodeVM({
    console: 'inherit',
    sandbox: {},
    require: {
        external: true,
        builtin: ['fs', 'path'],
        root: "./",
        mock: {
            fs: {
                readFileSync() { return 'Nice try!'; }
            }
        }
    }
});