patriksimek / vm2

Advanced vm/sandbox for Node.js
MIT License
3.87k stars 295 forks source link

Module not found error for some reason #454

Closed StNekroman closed 2 years ago

StNekroman commented 2 years ago

When I code inside vm2 (which is inside thread worker) I receive "Module not found error" Module is present in host project inside /node_modules/ as "npm link" I have a workaround:

builtin: ["fs", "path", "util", "discord-text-games-api", "discord-text-games-api/dto", "discord-text-games-api/events"],
resolve: (moduleName: string, parentDirname: string) : string | undefined => {
            if (moduleName === "discord-text-games-api" || moduleName === "discord-text-games-api/events") {
                return path.join(process.cwd(), "node_modules", "discord-text-games-api");
            }
        }

With workaround I manually require (from host scope) that module and pass it down. Without workaround - it cannot locate module. Any thoughts?

StNekroman commented 2 years ago

Ah, after debuging I found, that VM2's root property restricts access to hosts's node_modules.