PhantomJS抛出错误(rasterize.js)

时间:2015-10-05 10:32:14

标签: node.js phantomjs

我正在MEAN堆栈中尝试使用phantomjs来生成报告,但是在我的ubuntu os服务器中安装幻像二进制文件并使用幻像node_module + rasterize.js后会抛出错误。

Error: Cannot find module 'events'

phantomjs://bootstrap.js:254 in require
/home/kb/code/backend/backend/api_server/node_modules/phantom/node_modules/dnode/node_modules/dnode-protocol/index.js:1
/home/kb/code/backend/backend/api_server/node_modules/phantom/node_modules/dnode/node_modules/dnode-protocol/index.js:126
Error: Cannot find module 'stream'

phantomjs://bootstrap.js:289
phantomjs://bootstrap.js:254 in require
/home/kb/code/backend/backend/api_server/node_modules/phantom/node_modules/dnode/lib/dnode.js:2
/home/kb/code/backend/backend/api_server/node_modules/phantom/node_modules/dnode/lib/dnode.js:154
Error: Cannot find module 'net'

phantomjs://bootstrap.js:289
phantomjs://bootstrap.js:254 in require
/home/kb/code/backend/backend/api_server/node_modules/phantom/node_modules/dnode/index.js:3
/home/kb/code/backend/backend/api_server/node_modules/phantom/node_modules/dnode/index.js:138
Error: Cannot find module 'http'

phantomjs://bootstrap.js:289
phantomjs://bootstrap.js:254 in require
/home/kb/code/backend/backend/api_server/node_modules/phantom/phantom.js:8
/home/kb/code/backend/backend/api_server/node_modules/phantom/phantom.js:193
/home/kb/code/backend/backend/api_server/node_modules/phantom/phantom.js:194
TypeError: 'undefined' is not a function (evaluating 'phantom.createWebPage()')

:/modules/webpage.js:905
report.js:7

这是我的测试代码。

var phantom = require('phantom');

phantom.create(function (ph) {
  ph.createPage(function (page) {
    page.open("http://www.google.com", function (status) {
      console.log("opened google? ", status);
      page.evaluate(function () { return document.title; }, function (result) {
        console.log('Page title is ' + result);
        ph.exit();
      });
    });
  });
});

1 个答案:

答案 0 :(得分:1)

尝试运行命令: npm install -g events stream net http

但是,我认为您设置节点项目的方法不对。例如,如果要创建项目需要PhantomJS。您应该执行以下步骤:

# create project directory
mkdir myproject
cd myproject

# install phantomjs for your project
npm install --save phantomjs

# create your project file