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

globalThis of require'd modules is unreachable if inside vm context #4545

Open
2 tasks done
aecepoglu opened this issue Feb 12, 2025 · 1 comment
Open
2 tasks done

Comments

@aecepoglu
Copy link

aecepoglu commented Feb 12, 2025

Node.js Version

v22.12.0

NPM Version

v11.0.0

Operating System

Linux

Subsystem

vm

Description

How do I require() modules from an isolate vm context (created via vm.createContext({})) while also capturing the globalThis accesses of require'd modules?

The builtin REPLServer has the same issue and the sample code below demoes it: the globalThis of a module I'm requireing is out of my reach.

I know that useGlobal: false creates a new vm context and performs a vm.runInContext() as opposed to a vm.runInThisContext() . If I do the same myself the result is the same.

I've traced this to compileFunctionForCJSLoader, which is an internal function and I don't see where it's being passed a context.

Minimal Reproduction

// b.js
globalThis.one = 111
console.log("two", globalThis.two)
// my-repl.js
const {REPLServer} = require('node:repl')
const ss = new REPLServer({
  useGlobal: false
})
  1. node my-repl.js
  2. globalThis.two = 222
  3. require('./b')
  4. globalThis.one
  5. I expect to see that one is 111, and that the value of "two" is logged as 222. But both are undefined

Output

$/> node my-repl.js
> globalThis.two = 222
222
> require('./b')
two undefined
{}
> globalThis.one
undefined
>
(To exit, press Ctrl+C again or Ctrl+D or type .exit)
>

Before You Submit

  • I have looked for issues that already exist before submitting this
  • My issue follows the guidelines in the README file, and follows the 'How to ask a good question' guide at https://stackoverflow.com/help/how-to-ask
@aecepoglu
Copy link
Author

seems to be a duplicate of nodejs/node#56575

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

1 participant