neda neda - 2 months ago 24
Javascript Question

Execute node.js child process in browser by browserify

I am using browserify to get node.js to run on the browser. I want to execute a child process so I am doing something like this in the index.js

var exec = require('child_process').exec;
//I'm just checking the node version installed, you can do your own process here

var ls =exec('node -v', function(error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);

A bundle.js is generated using browserify command

browserify index.js -o bundle.js -d

Also included the bundle.js in html

<script src="bundle.js"></script>

But in the browser's console I am getting as

"exec is not a function"

Node version is v0.12.7


browserify does NOT run node.js in the browser.

Browserify lets you require('modules') in the browser.

so your code is nice and tidy. But, there is no child_process, net or fs.

Once again, you are NOT running node on the browser.

P.S. There are modules that are implementations of net and fs for the browser though such as browserify-fs etc.