node.js:jsdom问题-jQuery对象返回未定义 [英] node.js: problems with jsdom - jQuery object returns undefined

查看:227
本文介绍了node.js:jsdom问题-jQuery对象返回未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有兴趣为其中一个项目做一些服务器端DOM操作,因此我决定看看 node.js ,尤其是 jsdom .我是Node.js的新手,所以我的错误可能很明显,但是我从来没有在任何地方找到解决问题的方法...

首先,我在Windows本地运行节点,并且还使用在线IDE cloud9

值得一提的是,由于无法为节点安装最新的jsdom软件包(0.2.15),因为必需的依赖项 jsdom github repo cloud9的简单测试脚本时,cloud9返回了此警告

The "sys" module is now called "util". It should have a similar

我试图修复此问题,将所有提到的sys都替换为util,错误消失了……(仍然不确定这样做是否正确……).所以,最后,当我尝试运行此代码段

var jsdom = require("jsdom");

jsdom.env("http://nodejs.org/dist/", [
  'http://code.jquery.com/jquery-1.5.min.js'
],
function(errors, window) {
  console.log("there have been", window.$("a").length, "nodejs releases!");
});

我发现了window.$ === undefined.尝试在cloud9上与节点v0.6.x和节点v0.4.x一起运行,并在本地与节点v0.8.7一起运行.

  • 我试图做的另一件事是安装最新的jsdom@0.2.15,从在此处,安装绑定并将其添加到contextify/node_modules中,因为contextify依赖于此.我根本无法使它正常工作=(并没有浪费太多时间,因为这似乎是一种错误的方法,并且上下文化不是最新的.

    这是npm列表

    └─┬ jsdom@0.2.15
      ├─┬ contextify@0.1.2
      │ └── bindings@1.0.0
      ├── cssom@0.2.5
      ├── htmlparser@1.7.6
      └── request@2.10.0
    

  • 此外,这是我的 cloud9工作区,不要害羞.

    任何帮助表示赞赏.也许还有其他更好的方法可以满足我的要求.

    谢谢.

    解决方案

    Cheerio 是一个新模块不久前问世,它比jsdom使用起来简单得多,可以解决许多jsdom可以运行的问题.

    jsdom非常棒,但是如果您需要解析无效的HTML,则很难使用它,而必须使用 html5 解析器模块,该模块非常慢,而且所有开发人员都不友好.

    还有PhantomJS要考虑:
    http://phantomjs.org 我的引导程序开始使用jQuery.
    https://github.com/sgentle/phantomjs-node (这是 hack ,我无言以对)

    I am interested in doing some server-side DOM manipulations for one of projects, so I decided to take a look at node.js, and at jsdom in particular. I'm completely new to node.js, so probably my mistakes are obvious, but never the less i couldn't find solution for my problem anywhere...

    First of all i'm running node on Windows locally, and also use online IDE cloud9

    It was worth mentioning as i got troubles installing latest jsdom package for node (which is 0.2.15), because a required dependency contextify would not compile under Windows (C++ and Python required, and as I discovered npm does not support building C++ packages). So i took two alternative approaches:

    1. I've installed an older version that does not depend on contextify, that is jsdom@0.2.1. Here is npm list of what i currently have

      └─┬ jsdom@0.2.1
        ├── cssom@0.2.5
        ├── htmlparser@1.7.6
        └── request@2.10.0
      

      When i tried to run simple test script taken from jsdom github repo cloud9 returned this warning

      The "sys" module is now called "util". It should have a similar
      

      I tried to fix this replacing all the mentions of sys to util, error disappeared... (still not so sure it was right thing to do though...). And so, finally when I tried to run this snippet

      var jsdom = require("jsdom");
      
      jsdom.env("http://nodejs.org/dist/", [
        'http://code.jquery.com/jquery-1.5.min.js'
      ],
      function(errors, window) {
        console.log("there have been", window.$("a").length, "nodejs releases!");
      });
      

      I found out that window.$ === undefined. Tried to run with node v0.6.x and node v0.4.x on cloud9 and with node v0.8.7 locally.

    2. Another thing i tried to do was to install latest jsdom@0.2.15, get a compiled contextify from over here, install bindings and add it to contextify/node_modules as contextify is dependent on it. I was not able to make it work at all =( Didn't waste much time, as it seems to be a false approach, and contextify is not up to date.

      Here's npm list

      └─┬ jsdom@0.2.15
        ├─┬ contextify@0.1.2
        │ └── bindings@1.0.0
        ├── cssom@0.2.5
        ├── htmlparser@1.7.6
        └── request@2.10.0
      

    Also, here's my cloud9 workspace, don't be shy to play around.

    Any help appreciated. Maybe there are other better to accomplish what I wanted.

    Thanks.

    解决方案

    Cheerio is a new module that came out not a long time ago that is a lot more simple to use than jsdom and access many of the issue you can run with jsdom.

    jsdom is awesome but it can be really hard to use if you need to parse non valid HTML you have to play with the html5 parser module, which is super slow and the all thing is not dev friendly.

    There is also PhantomJS to consider:
    http://phantomjs.org my bootstrap to get started with jQuery.
    https://github.com/sgentle/phantomjs-node (this is such a hack, I'm speechless)

    这篇关于node.js:jsdom问题-jQuery对象返回未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆