Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected token in jsdom when used with babel and npx

I want to do some tests on canvas purely in node.js.

This is my package.json

{
  "name": "test",
  "description": "Test",
  "version": "0.1.0",
  "author": "anthony@work",
  "dependencies": {
    "canvas": "^1.6.7",
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-stage-0": "^6.24.1",
    "jsdom": "^11.3.0"
  }
}

This is my .babelrc

{
 "presets": ["env", "stage-0"]
}

This is my test javascript (a.js)

const { jsdom } = require('jsdom');

// main
jsdom();


console.log('done')

When I run the script, however,

 npx babel a.js | nodejs -- -

I get this error:

/work/node_modules/jsdom/lib/api.js:10
const { URL } = require("whatwg-url");
      ^

SyntaxError: Unexpected token {
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:374:25)
    at Object.Module._extensions..js (module.js:417:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Module.require (module.js:354:17)
    at require (internal/module.js:12:17)
    at [stdin]:3:16
    at Object.exports.runInThisContext (vm.js:54:17)
    at Object.<anonymous> ([stdin]-wrapper:6:22)

What causes this error? How can I fix this error?

Version information:

Node.js: v4.2.6 OS: Ubuntu 16.04

like image 389
Anthony Kong Avatar asked Feb 01 '26 08:02

Anthony Kong


1 Answers

You are running an incompatible node version with jsdom. jsdom@10 requires nodejs v6. So either upgrade Node or downgrade jsdom.

Found info about that here: https://github.com/tmpvar/jsdom#jsdom

like image 164
ThatsRight Avatar answered Feb 03 '26 20:02

ThatsRight



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!