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

  • SEARCH
  • Home
  • 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 6128589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:38:38+00:00 2026-05-23T16:38:38+00:00

Whenever I run vm.runInThisContext(code, filename) , the code I ran reports __filename and __dirname

  • 0

Whenever I run vm.runInThisContext(code, filename), the code I ran reports __filename and __dirname as undefined.

This also leads to the situation that any fs.readFile and such calls will not work with relative paths. Actually to be exact, file system functions do not work at all even if I feed them a hard-coded absolute path to an existing file.

For example, this will do nothing:

var fs = require('fs');
fs.readFile('/home/test/file.txt', function(e, data) {
    if (e) {throw e;}
    console.log('here i am');
});

What happens is that nothing happens. If I run the code in normal NodeJS code then it outputs “here i am”, but if I run that code through the vm module, then nothing happens. The callback is simply never called, because for some reason it can’t locate the file and there does not seem to be any timeouts either.

How can I make Node to understand that the executed code is some “file” and also make the fs module functions to work? I tried specifying the second parameter to vm.runInThisContext(code, filename), but I see no difference. It almost looks that Node doesn’t care about the second parameter.

  • 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-05-23T16:38:38+00:00Added an answer on May 23, 2026 at 4:38 pm

    I’m not exactly sure how I even got my code examples to work before, because right now they do not work at all.

    I found out that you can use vm.runInNewContext(code, sandbox, filename) and then specify require, __filename and whatever you need in the sandbox:

    // Place here some path to test it. I'm using Cygwin...
    var filename = '/cygdrive/z/www/project/src/bootstrap.js';
    var code = "var fs = require('fs'); fs.readFile('./path/to/some/file.js', function(e, data) {if (e) {throw e;} console.log(data.toString());});";
    
    var vm = require('vm');
    vm.runInNewContext(code, {
        require: require,
        console: console,
        __filename: filename
    }, filename);
    

    Then if I run node bootstrap.js --debug it works fine!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a few server reports that return usage statistics whenever run. The
Whenever I run this code, I get a same result. Program #include<stdlib.h> int main(int
Whenever I run this file the code runs up to the point where the
Whenever I run this, and open the color dialog, there are many colors that
I had developed an application in visual Studio 2005. whenever i run that application
Why whenever I compile and run the following code in Visual Studio 2008: double
Whenever I run the IntelliJ autoformatter - it converts this: @Autowired private CustomerDao customerDao;
I have the following code embeded in a class.Whenever I run distToPoint it gives
I am having a problem with my code. Whenever i run the program, the
I set up Cucumber+Webrat+Selenium according to this article . Whenever I run my server,

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.