"eval" function can't access current context

This issue has been tracked since 2022-12-31.

Describe the bug
eval function is not working as expected on the latest browserless images as it can't access the current context.

The problem is that our system (a +300 pods kubernetes cluster) heavily depends on eval function to execute user code and expose very specific variables as context that were generated by a very complex "base" function we designed. However, it seems the latest browserless versions seems to use vm2 npm module to mock and sandbox the eval function completely blocking any attempt to send a context which breaks our system function.

To Reproduce
Steps to reproduce the behavior using a ruby script:

  1. Start a docker container: docker run --rm -p 3000:3000 browserless/chrome:1-puppeteer-13.5.2
  2. Install the Ruby typhoeus gem for testing the request: gem install typhoeus
  3. Execute this Ruby script
require 'json'
require 'typhoeus'

body = {
  context: {},
  code: '
    module.exports = async ({ browser, page, context }) => {
      var my_var = "Hello"
      const data = eval("my_var")
      return {
        data: data,
        type: "application/json",

req = Typhoeus::Request.new(
  headers: {'Content-Type' => 'application/json'},
  method: :post,
  body: JSON.generate(body),
  timeout: 120
puts req.run.body
  1. You will get the error message "my_var is not defined"

Expected behavior
eval gets access to my_var and displays "Hello!" in the terminal.

Desktop (please complete the following information):

  • OS: "browserless/chrome:latest" docker image
  • Browser Chrome
  • Version e1c84725a2e7bf04f53e7d738f144cb5951aeb28570ba6e9d67929402b0367b8

Additional context
We are currently using a very old image that is working fine: browserless/chrome:1.42.0-puppeteer-5.4.1

andyMrtnzP wrote this answer on 2023-01-04

We use vm2 to execute custom code. It's a known issue that, unfortunately, direct eval() calls don't work on vm2.

I you really need eval, by default, custom code doesn't run in strict mode, so you can declare the vars globally and access them via eval():

module.exports = async () => {
  my_var = `Hello`;
  const data = eval(`my_var`);
  return { data: data, type: `application/json` };
More Details About Repo
Owner Name browserless
Repo Name chrome
Full Name browserless/chrome
Language TypeScript
Created Date 2017-11-17
Updated Date 2023-03-22
Star Count 5309
Watcher Count 47
Fork Count 516
Issue Count 29


Issue Title Created Date Updated Date