Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't seem to like vm2 b/c of a library 'contextify' #495

Closed
aubelsb2 opened this issue Oct 30, 2020 · 3 comments
Closed

Doesn't seem to like vm2 b/c of a library 'contextify' #495

aubelsb2 opened this issue Oct 30, 2020 · 3 comments

Comments

@aubelsb2
Copy link

Perhaps this is a problem with VM2 and the way it manages dependencies (however a bit of digging seems to suggest that contextify.js is now internal to node.js?)

Eitherway with this setup:
package.json

{
  "name": "esbuildvm2",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "vm2": "^3.9.2",
    "esbuild": "^0.8.0"
  },
  "dependencies": {
  },
  "scripts": {
    "build": "esbuild --bundle --outfile=./bundle.js --target=es2020 --platform=node --minify --loader:.ts=ts ./main.ts"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "strict": true,
    "module": "commonjs",
    "lib": ["es2017", "es7", "es6"],
    "target": "es6",
    "sourceMap": true,
    "declaration": true,
    "outDir": "dist",
    "esModuleInterop": true
  },
  "include": ["**/*.ts"],
  "exclude": [
    "**/*.test.ts",
    "**/*.tests.ts",
    "node_modules",
    "dist"
  ]
}

main.ts

import {VM} from 'vm2';

const vm = new VM({
    timeout: 1000,
    sandbox: {}
});
let number = vm.run("1337"); // returns 1337
console.log(number);

I get these results when I try build / run it:

C:\Users\arran_w\WebstormProjects\esbuildvm2>npm run build

> esbuildvm2@1.0.0 build C:\Users\arran_w\WebstormProjects\esbuildvm2
> esbuild --bundle --outfile=./bundle.js --target=es2020 --platform=node --minify --loader:.ts=ts ./main.ts

node_modules/vm2/lib/main.js:638:102: warning: Indirect calls to "require" will not be bundled (surround with a try/catch to silence this warning)
    const _internal = CACHE.contextifyScript.runInContext(_context, DEFAULT_RUN_OPTIONS).call(_context, require, HOST);
                                                                                                        ~~~~~~~
node_modules/vm2/lib/main.js:1226:1: warning: Indirect calls to "require" will not be bundled (surround with a try/catch to silence this warning)
  require,
  ~~~~~~~
2 warnings

C:\Users\arran_w\WebstormProjects\esbuildvm2>node bundle.js
internal/fs/utils.js:230
    throw err;
    ^

Error: ENOENT: no such file or directory, open 'C:\Users\arran_w\WebstormProjects\esbuildvm2/contextify.js'
←[90m    at Object.openSync (fs.js:462:3)←[39m
←[90m    at Object.readFileSync (fs.js:364:35)←[39m
    at _ (C:\Users\arran_w\WebstormProjects\esbuildvm2\bundle.js:1:901)
    at C:\Users\arran_w\WebstormProjects\esbuildvm2\bundle.js:1:1072
    at C:\Users\arran_w\WebstormProjects\esbuildvm2\bundle.js:1:233
    at C:\Users\arran_w\WebstormProjects\esbuildvm2\bundle.js:6:1590
    at C:\Users\arran_w\WebstormProjects\esbuildvm2\bundle.js:1:233
    at Object.<anonymous> (C:\Users\arran_w\WebstormProjects\esbuildvm2\bundle.js:6:1606)
←[90m    at Module._compile (internal/modules/cjs/loader.js:1147:30)←[39m
←[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:10)←[39m {
  errno: ←[33m-4058←[39m,
  syscall: ←[32m'open'←[39m,
  code: ←[32m'ENOENT'←[39m,
  path: ←[32m'C:\\Users\\arran_w\\WebstormProjects\\esbuildvm2/contextify.js'←[39m
}

C:\Users\arran_w\WebstormProjects\esbuildvm2>

@evanw
Copy link
Owner

evanw commented Oct 31, 2020

It looks like this library is kind of incompatible with bundling. It loads content from the file system at run-time using fs.readFileSync() and that content won't be a part of the bundle.

If it's not too much trouble, I believe you can get this to work by manually copying the file node_modules/vm2/lib/contextify.js next to the bundled output file that esbuild generates. This should be enough to get vm2 to work since it looks for __dirname + '/contextify.js' at run-time.

@evanw
Copy link
Owner

evanw commented Nov 7, 2020

I'm assuming this is answered because of the 👍. Closing.

@evanw evanw closed this as completed Nov 7, 2020
@aubelsb2
Copy link
Author

Actually that was to thanks for the comment I hadn't tested it until now.. Turns out I also needed sandbox.js. I guess this isn't something which will change any time soon?

But thanks I can confirm now that the work around worked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants