Skip to content

can't assign function to global variable ( code execution happens inside script.runInContext) #2094

Closed
@joxerTMD

Description

@joxerTMD

Hi i'm facing a very strange behavior and would be glad for some help or explanation

  • Node.js Version: v12.1.0
  • OS:
    System Version: macOS 10.13.6 (17G2208)
    Kernel Version: Darwin 17.7.0
  • Scope (install, code, runtime, meta, other?): runtime
  • Module (and version) (if relevant):

This code fails during execution (i'm trying to assign a function inside isolated context)

const vm = require('vm')
const codeString = `
'use strict';
exports = function someFunc() {}
`
const s = new vm.Script(codeString);
s.runInContext(vm.createContext({
    exports: {}
}));

The error is:

ReferenceError: exports is not defined
    at evalmachine.<anonymous>:3:9
    at Script.runInContext (vm.js:134:20)
    at Object.<anonymous>

looks like its somehow looses context passed with

vm.createContext({
    exports: {}
})

however this code completes with no errors ( assign someString instead of function)...

const vm = require('vm')
const codeString = `
'use strict';
exports = 'someString'
`
const s = new vm.Script(codeString);
s.runInContext(vm.createContext({
    exports: {}
}));

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions