browserify Error : http.createServer is not a func

2019-06-05 08:34发布

I tried to browserify this node js script :

var phantom = require('phantom')
phantom.create(function(ph) {
ph.createPage(function(page) {
    page.open("editor.html", function(status) {
        console.log("opened diagram? ", status);
        page.evaluate(function() {
            return document.getElementById("GraphImage").src;
        }, function(result) {
            //console.log(result);
            ph.exit();
        });
    });
});
});

So I used this command:

browserify myscript.js > bundle.js

and when I run bundle.js from an html file I get this error:

http.createServer is not a function 

it seems that browserify does not support httpserver. How can I resolve this problem?

1条回答
ゆ 、 Hurt°
2楼-- · 2019-06-05 09:18

You can't run a web server from inside a web browser. There really isn't anything in the browser that could act like Node's http module. Also it doesn't make sense to run PhantomJS in a browser, because PhantomJS is a web browser.

What is the desired behavior you are trying to accomplish?


Update:

It seems like you are trying to run code intended for Node.js inside a browser instead.

The JavaScript engine inside the browser is much more restrictive than in Node.js, for example you can't access the file system from inside the browser for security reasons (or else you could read the hard drive of anyone who visited your web page).

Browserify does include some "shims" that will put small JS libraries into your code that work in the browser and match the API of Node.js, allowing some Node.js specific JS code to execute in the browser.

In your case, you are requiring Phantom, which seems to in turn require http. Accoring to the Browserify documentation, it will see require('http') and include a shim for the http module (because browser's don't provide an http module of their own).

The Phantom module then tries to call http.createServer() but accoring to the documentation for that http shim:

This module implements http.request, http.get, and most of http.ClientRequest and http.IncomingMessage in addition to http.METHODS and http.STATUS_CODES.

so http.createServer() is not supported by the shim. This also makes sense because a browser would never let you open an http server inside of itself, or else navigation to someone's web site could cause your browser to start serving content to the outside world, which obviously doesn't make sense.

In your comment:

"i want that my node js script can be executed from another JS code"

You don't specify what "other JS code" is running in. If that JS code is already running in Node, then you don't need Browserify at all. If you are trying to have a web browser start up an actual Node.js process, that isn't going to happen, again for obvious security reasons, because browsing to a web page shouldn't have the ability to run any executable on your system.

What Browserify lets you do is take code originally intended for Node.js, and run it in a browser instead, but a t runtime it is executing in the browser, not in Node.js, so you can only use JS code that works within the constraints of the browser's JS runtime.

If you are trying to execute your code in Node.js, then you need to do that by having something start the Node.js executable, either from the command line or by having another program start the process for you, but that can't be done from within a web browser. If you are trying to have users navigate to your web site and then have this code run on their machines in a browser and not in Node.js, then you need to only use modules that work in the browser.

查看更多
登录 后发表回答