Transform stream that executes JavaScript it receives in a real browser and outputs console output


The easiest way of running code in a browser environment.

Bundles electronjs by default!

$ echo "console.log('Hey from ' + location); window.close()" | browser-run
Hey from http://localhost:53227/

Or use browser-run programmatically:

var run = require('browser-run');
var browser = run();
browser.end('console.log(location); window.close()');
$ browserify main.js | browser-run


var browserify = require('browserify');
var browser = require('browser-run');
$ browser-run --help
Run JavaScript in a browser.
Write code to stdin and receive console output on stdout.
Usage: browser-run [OPTIONS]
  --browser, -b  Browser to use. Always available: electron. Available if installed: chrome, firefox, ie, phantom, safari  [default: "electron"]
  --port         Starts listening on that port and waits for you to open a browser                                       
  --static       Serve static assets from this directory                                                                 
  --input        Input type. Defaults to 'javascript', can be set to 'html'.                                             
  --help         Print help                                                                                              

By using -input html or { input: 'html' } you can provide a custom html file for browser-run to use. Keep in mind though that it always needs to have <script src="/reporter.js"></script> above other script tags so browser-run is able to properly forward your console.logs etc to the terminal.

Returns a duplex stream and starts a webserver.

opts can be:

  • port: If speficied, no browser will be started, so you can point one yourself to http://localhost/<port>
  • browser: Browser to use. Defaults to electron. Available if installed:
    • chrome
    • firefox
    • ie
    • phantom
    • safari
  • static: Serve static files from this directory
  • input: Input type. Defaults to javascript, can be set to html.

If only an empty string is written to it, an error will be thrown as there is nothing to execute.

If you call window.close() inside the script, the browser will exit.

Stop the underlying webserver.

With npm do

$ npm install browser-run    # for library
$ npm install -g browser-run # for cli