0
votes

I am a beginner to Browserify for NodeJS and I wrote a simple module that would list the network interfaces and return it to the person viewing a webpage. I begun by writing the main.js file,

var os = require("os");
document.write(os.networkInterfaces());

Then I compiled the code via this command browserify main.js -o bundle.js which on completion didn't yield any output but resulted in the creation of bundle.js. This is when the html code comes into play,

<html>
    <body>
        Hi, here are your network interfaces:
        <script src="bundle.js"></script>
    </body>
</html>

Upon loading the page it says, Hi, here are your network interfaces:[object Object] when I was expecting a string that NodeJS says it's os.networkInterfaces() function returns when called. What did I do wrong and how can I go about fixing it?

1

1 Answers

1
votes

That has nothing to do with browserify.
document.write tries to stringify an object by calling the Object#toString() function of that object's prototype.

So, if you want to print a string use JSON.stringify(obj, null, 2) (2 is for 2 spaces, for prettification)

var os = require("os");
document.write(JSON.stringify(os.networkInterfaces(), null, 2)));

Note: you can check in console by typing:

console.log({"foo":"bar"}.toString());

Also, you need to use os-browserify if you want to use os module in browsers