简体   繁体   中英

How to allow filesystem access

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.

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!'; }
            }
        }
    }
});

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