找不到模块'网页'在MeteorJS中使用PhantomJS时

时间:2015-03-05 07:21:52

标签: node.js meteor phantomjs

我通过phantom包使用meteorhacks:npm npm包。但是当在Meteor.js下运行基本的Phantomjs示例时,我收到Cannot find 'webpage'错误。

为什么会这样?

代码

var phantomjs = Meteor.npmRequire('phantom')
var page = Npm.require('webpage').create();
page.open('http://github.com/', function() {
    console.log('Page Loaded');
    phantom.exit();
});

错误

W20150305-02:16:51.629(-5)? (STDERR) Error: Cannot find module 'webpage'
W20150305-02:16:51.629(-5)? (STDERR)     at Function.Module._resolveFilename (module.js:338:15)
W20150305-02:16:51.629(-5)? (STDERR)     at Function.Module._load (module.js:280:25)
W20150305-02:16:51.629(-5)? (STDERR)     at Module.require (module.js:364:17)
W20150305-02:16:51.629(-5)? (STDERR)     at require (module.js:380:17)
W20150305-02:16:51.629(-5)? (STDERR)     at Object.Npm.require (/Users/username/Code/phantomtest/.meteor/local/build/programs/server/boot.js:129:18)
W20150305-02:16:51.629(-5)? (STDERR)     at app/server/phantom.js:8:16
W20150305-02:16:51.629(-5)? (STDERR)     at app/server/phantom.js:26:3
W20150305-02:16:51.629(-5)? (STDERR)     at /Users/username/Code/phantomtest/.meteor/local/build/programs/server/boot.js:205:10
W20150305-02:16:51.629(-5)? (STDERR)     at Array.forEach (native)
W20150305-02:16:51.630(-5)? (STDERR)     at Function._.each._.forEach (/Users/username/.meteor/packages/meteor-tool/.1.0.41.1f49rvw++os.osx.x86_64+web.browser+web.cordova/meteor-tool-os.osx.x86_64/dev_bundle/server-lib/node_modules/underscore/underscore.js:79:11)

packages.json

{
    "phantom": "0.7.2"
}

1 个答案:

答案 0 :(得分:6)

网页是PhantomJS模块,而不是NPM包。 PhantomJS和node.js具有不同的运行时,这就是您需要使用像幻像包这样的桥的原因。 phantom本身将提供对page

的引用
var phantom = Meteor.npmRequire('phantom')
phantom.create(function (ph) {
  ph.createPage(function (page) {
    page.open("http://github.com/", function (status) {
      console.log('Page Loaded');
      ph.exit();
    });
  });
});

请记住,为桥构建的脚本必须使用简单的PhantomJS脚本编写different