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 8665883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:35:35+00:00 2026-06-12T17:35:35+00:00

In node.js vm module , i can execute some javascript in another node.js process.

  • 0

In node.js vm module, i can execute some javascript in another node.js process. What i want to achieve is being able to catch logs performed in the script runned by the vm.

For example i want to catch the “foobar” log:

var vm = require('vm')

vm.runInThisContext('console.log("foobar");', 'myfile.vm');

// how can i get the "foobar" log?

Thanks in advance

EDIT: The answer below works well, although there’s a shorter version:

function captureStdout(callback) {
    var output = '', old_write = process.stdout.write

    // start capture
    process.stdout.write = function(str, encoding, fd) {
        output += str
    }

    callback()

    // end capture
    process.stdout.write = old_write

    return output
}
  • 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-12T17:35:36+00:00Added an answer on June 12, 2026 at 5:35 pm

    You can just wrap console.log directly:

    function hook_consolelog(callback) {
        var old_log = console.log;
    
        console.log = (function(write) {
            return function() {
                write.apply(console, arguments)
                callback.apply(null, arguments);
            }
        })(console.log)
    
        return function() {
            console.log = old_log;
        }
    }
    
    var result;
    var unhook = hook_consolelog(function(v) {
        result = v;
    });
    
    console.log('hello');
    unhook();
    console.log('goodbye');
    console.log('the result is ', result);​
    

    Since console.log simply calls process.stdout, another approach would be to capture the stdout events using a bit of wrapper magic like this:

    var util = require('util')
    
    function hook_stdout(callback) {
        var old_write = process.stdout.write
    
        process.stdout.write = (function(write) {
            return function(string, encoding, fd) {
                write.apply(process.stdout, arguments)
                callback(string, encoding, fd)
            }
        })(process.stdout.write)
    
        return function() {
            process.stdout.write = old_write
        }
    }
    
    var unhook = hook_stdout(function(string, encoding, fd) {
        util.debug('stdout: ' + util.inspect(string))
        if( string == 'foobar' ) { unhook(); }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a native (c++) module for node.js which is able to
I've created a node module that is essentially just some useful JS that can
I'm looking for a node.js module that can generate and parse mime multipart/related http
how can i manage communication with the database in node.js? is there any module
I'm trying out the node-mysql module on node js. What I want to do
I want to ask something about the new module 'Domain' in node v0.8. var
We are looking for a Javascript test framework that can execute tests across the
Is there any node.js module which can do mac address filtering . Basically I
is there any node module that I can use to analyze the log file?
I'm sure this is simple with the Node Relationship module but I can't wrap

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.