Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8628681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:36:31+00:00 2026-06-12T08:36:31+00:00

Can someone help me understand the following behavior? I’d expect that, since I can

  • 0

Can someone help me understand the following behavior? I’d expect that, since I can set the global f from inside this callback, I should also be able to change it. I don’t understand well enough how node handles context vs. global in the REPL to make sense of this, and I’d appreciate any insight.

Start a REPL without useGlobal

$ cat test.js 
var repl = require('repl');
repl.start({useGlobal:false});

Now try setting f twice in a row:

$ node test.js 
> f
ReferenceError: f is not defined
>  setTimeout(function(){f=1;}, 0);
> f
1

Works the first time… now try again:

>  setTimeout(function(){f=2;}, 0);
> f
1

Huh!

The first run sets it; the second doesn’t affect it.

(Setting useGlobal:true I get the behavior I expect.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:36:32+00:00Added an answer on June 12, 2026 at 8:36 am

    It’s because of Node’s faulty vim module which is what’s running your code behind the scenes. When you disable useGlobal this changes the manner in which it runs your code, changing from vm.runInThisContext to vm.runInContext [1]. Essentially what happens is that it copies all the variables from the specified sandbox object over to the actual global object it runs your code in, executes the code, then copies everything back. When you use setTimeout the change is made after everything is copied back. The blame for this lies partially in the repl module [2] and partially in the vm module [3]. I believe the vm module is slated for an overhaul for the next version.

    • [1] https://github.com/joyent/node/blob/master/lib/repl.js#L111
    • [2] https://github.com/joyent/node/blob/master/lib/repl.js#L341
    • [3] https://github.com/joyent/node/blob/master/src/node_script.cc#L114
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Java docs says the following about Set interface, can someone please help me understand
Can someone help me understand how to write this case statement properly its not
Can someone please help me understand the following: In the previous version of NHibernate
I'm trying to better understand CSS. Can someone help me out with this. I
Can someone help me understand how OpenID works? I'm interested in the following answers:
Can someone help me understand how to convert this sql query to a named_scope
I'm wondering if someone can help me to understand the following command. The purpose
I'm wondering if someone can help me understand the following behaviour. If I have
Can someone help me understand why the following query is not offsetting correctly? It's
(Specifically, I'm using Backbone Model events) Can someone help me understand how javascript events

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.