关于Node js和phantomjs:Node js和phantomjs-找不到模块’弱’

Node js and phantomjs - Cannot find module 'weak'

我正在64位Linux Mint 16上运行" petra",并且尝试与节点js一起首次使用" phantomjs"。

我已经在全球范围内安装了phantomjs:

1
sudo npm install -g phantomjs

...并确认它正在运行(我通过在终端中运行" phantomjs"得到phantomjs提示)

并且我已经在节点项目中安装了节点" phantom"模块:

1
npm install phantom

到现在为止还挺好。

但是,在我的应用程序代码中,它尝试执行此行时:

1
var phantom = require('phantom');

...程序因以下跟踪信息而崩溃:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Listening on port 3000
about to instantiate phantom module...
module.js:333
    throw err;
          ^
Error: Cannot find module 'weak'
    at Function.Module._resolveFilename (module.js:331:15)
    at Function.Module._load (module.js:273:25)
    at Module.require (module.js:357:17)
    at require (module.js:373:17)
    at new D (/home/joe/Documents/My Stuff/Programming/Angular.js Projects/NodeJS Messing/FreeAgentScraper/node_modules/phantom/node_modules/dnode/index.js:28:20)
    at module.exports (/home/joe/Documents/My Stuff/Programming/Angular.js Projects/NodeJS Messing/FreeAgentScraper/node_modules/phantom/node_modules/dnode/index.js:8:12)
    at /home/joe/Documents/My Stuff/Programming/Angular.js Projects/NodeJS Messing/FreeAgentScraper/node_modules/phantom/phantom.js:135:13
    at Server.handler (/home/joe/Documents/My Stuff/Programming/Angular.js Projects/NodeJS Messing/FreeAgentScraper/node_modules/phantom/node_modules/shoe/index.js:22:9)
    at Server.EventEmitter.emit (events.js:104:17)
    at App.emit (/home/joe/Documents/My Stuff/Programming/Angular.js Projects/NodeJS Messing/FreeAgentScraper/node_modules/phantom/node_modules/shoe/node_modules/sockjs/lib/sockjs.js:182:27)
    at Session.emit_open (/home/joe/Documents/My Stuff/Programming/Angular.js Projects/NodeJS Messing/FreeAgentScraper/node_modules/phantom/node_modules/shoe/node_modules/sockjs/lib/transport.js:107:23)

我可以确认确实在项目中的任何地方都没有" weak.js"。

我已经运行" npm install"以确保已安装所有依赖项。

Google搜索没有发现任何值得的东西。 谁能提供任何建议?


我在Windows机器上使用node.js和phantom时遇到了相同的问题,我发现您需要在Windows环境中进行一些特殊处理。 npm文件在这里。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var phantom = require('phantom');
phantom.create(function(ph) {
    return ph.createPage(function(page) {
      return page.open("http://www.google.com", function(status) {
         console.log("opened google?", status);
         return page.evaluate((function() {
            return document.title;
      }), function(result) {
        console.log('Page title is ' + result);
        return ph.exit();
      });
    });
  });
}, {
    dnodeOpts: {weak: false}
});

注意phantom.create(function,options,callback),这里的选项使用dnodeOpts:{weak:false}


您是否在系统中安装了build-essential?因为弱是需要本地编译的模块。一周前,我通过安装build-essential(sudo apt-get install build-essential)使它在Ubuntu系统中工作,但是现在当我在Linux Mint中尝试时,它给了我同样的错误。在npm install -g phantom期间,它警告无法安装weak 0.3.1

您找到任何解决方案了吗?

编辑

而且,顺便说一下,我成功的系统与当前系统之间的关键区别在于,第一个是32位,而第二个是失败的是64位。是否模块"弱"有任何问题使其无法安装在64位盒上?

解决方案

通过执行以下步骤可以解决此问题:

  • sudo add-apt-repository ppa:fkrull/deadsnakes
  • sudo apt-get update
  • sudo apt-get install python2.6
  • sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.6 20
  • python --version应该给您2.6.x。当Python版本高于2.6.x时,不会安装弱模块。
  • 之后,请确保您的节点和npm是最新版本。我有节点v0.10.28和npm v1.4.9。

    如果您已经安装了幻影模块,请通过运行npm uninstall phantom将其删除。然后运行npm cache clean

    现在,分别安装weak:npm install weak。如果发生这种情况,请通过运行npm install phantom安装phantom。

    那应该解决问题。


    我遇到了同样的问题,此代码解决了我的问题。
    幻影版本= 1.9.8
    Phantom npm模块版本= 0.8.4

    `

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    var phantom=require('phantom');
    phantom.create(function( ph ){  
        },{
            dnodeOpts: {
                weak: false
            },
            parameters:{
                'web-security': 'no'
            }
    });

    `