2

I am really new to nodejs, and i am putting myself through a crash course these 3-4 days to get a bit better at it... Trying to run this code:

(function () {

    var jsdom = require('jsdom');

    jsdom.env({  
      html: '<html><body><h1>Hello World!</h1><p class="hello">Heya Big World!</body></html>'
      //scripts: [
        //'http://code.jquery.com/jquery-1.5.min.js'
      //]
    }, function (err, window) {
      //var $ = window.jQuery;
         var $ = require('jquery')(window)
      $('body').append("<div class='testing'>Hello World</div>");
      console.log($(".hello").text()); // outputs Hello World
    });

}());

I get this error:

/root/node_modules/jsdom/lib/jsdom/living/helpers/validate-names.js:2
const xnv = require("xml-name-validator");
^^^^^
SyntaxError: Use of const in strict mode.
    at exports.runInThisContext (vm.js:73:16)
    at Module._compile (module.js:443:25)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (/root/node_modules/jsdom/lib/jsdom/level1/core.js:8:20)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10

And i am clueless to solve it. Sorry for the nooby question...

Evan
  • 1,683
  • 7
  • 35
  • 65
  • kindly refer to http://stackoverflow.com/questions/22603078/syntaxerror-use-of-const-in-strict-mode – Zuned Ahmed Feb 27 '15 at 01:55
  • @ZunedAhmed I am not using strict mode, its being used in a module... id really rather not edit module source code... – Evan Feb 27 '15 at 01:56
  • Note that NodeJS isn't supported any more by jsdom (you can use a legacy version) - jsdom considers NodeJS legacy software and only support io.js as of version 4. https://github.com/tmpvar/jsdom/blob/master/Changelog.md – Benjamin Gruenbaum Feb 27 '15 at 01:59
  • 1
    You can change `const` to `var`. It also might work if you start node.js with the `--harmony` flag. And, what version of node.js are you running? – jfriend00 Feb 27 '15 at 02:45
  • @jfriend00 Thank you, i will let you know when i test that if its correct so you can add that as an answer. – Evan Feb 27 '15 at 03:00

2 Answers2

4

For me help reinstall jsdom from "latest" to old version "3.1.2"

1) Delete jsdom folder in node_modules

2) Change dependencies in package.json to:

"jsdom": "3.1.2"

3) And of course run "npm install"

Hope it's help you!

Roman Dunin
  • 56
  • 1
  • 1
4

The lastest version of jsdom no longer works with Node.js. This is from their github page.

Note that as of our 4.0.0 release, jsdom no longer works with Node.js™, and instead requires io.js. You are still welcome to install a release in the 3.x series if you are stuck on legacy technology like Node.js™

So as Roman Dunin said, roll back the to the 3.x version if you want to use jsdom with Node.js. Version 3.1.2 was the last release before version 4.0.0

Jack Vial
  • 2,354
  • 1
  • 28
  • 30