jbastias
jbastias

Reputation: 129

casperjs unable to run CoffeeScript scripts

I installed casperjs for the first time and ran the sample scripts. The first sample script (javascript) ran without incident. Then i tried running a coffescript sample, which I prefer, and received the following error:

Unable to load script test.coffee; check file syntax

I searched for an answer and the solution in the only related issue didn't work for me. I was able to compile the CoffeeScript (test.coffee) into JavaScript (test.js) and then ran the compiled JavaScript, again, without indecent.

I tried to track down the error by searching for the error message in the casperjs files. I found the error message at the end the ~/.node/lib/node_modules/casperjs/bin/bootstrap.js file where it passes control to phantomjs. I created simple a CoffeeScript: test_phantomjs.coffee:

console.log "hello phantomjs"
phantom.exit()

and ran the script (phantomjs test_phantomjs.coffee) with the following result:

Can't open 'test_phantomjs.coffee'

At this point I'm at loss. The problem is more of an inconvenience than anything since compiling into JavaScript solves the issue. Is their something I'm missing?

Upvotes: 8

Views: 1135

Answers (4)

ymkjp
ymkjp

Reputation: 91

As others say, PhantomJS v2.x no longer supports CoffeeScript.

Therefore, now you have 2 options to run PhantomJS with CoffeeScript:

  • Run plain *.coffee files with PhantomJS v1.9.8
  • Compile *.coffee files to *.js, and run *.js with PhantomJS v2.x

I recommend the latter up-to-date way, and this is how package.json looks like:

{
    "scripts": {
        "pretest": "npm install && coffee --compile **/*.coffee",
        "test": "casperjs test --fail-fast script/*.js",
        "watch": "coffee --watch --compile **/*.coffee"
    }
}

See more detail at ymkjp/phantomjs2x_coffee_sample.

Btw, here's the way to install PhantomJS v1.9.8 on Ubuntu.

$ sudo apt-get update
$ sudo apt-get install build-essential g++ flex bison gperf ruby perl \
  libsqlite3-dev libfontconfig1-dev libicu-dev libfreetype6 libssl-dev \
  libpng-dev libjpeg-dev python libx11-dev libxext-dev git
$ cd ~
$ wget -O- https://github.com/ariya/phantomjs/archive/1.9.8.tar.gz | tar zxvf -
$ cd ~/phantomjs-1.9.8
$ bash build.sh  # It takes 30 min or so (Up to your host machine)
$ sudo ln -s ~/phantomjs-1.9.8/bin/phantomjs /usr/local/bin
$ phantomjs --version
1.9.8

Cheers.

Upvotes: 0

Mickaël Andrieu
Mickaël Andrieu

Reputation: 81

(disclaimer: i'm/was part of contribution team) the support of coffee script in CasperJS depends on the version of phantomJs you are using because this is phantomjs that provide coffee script support out of box.

You can also use and install slimerjs (https://slimerjs.org) which - from my point of view - is more performant than phantomjs and supports coffeescript scripts.

Mickaël

Upvotes: 1

RDPanek
RDPanek

Reputation: 96

In Phantomjs2.0 was removed support of coffee-script

https://github.com/ariya/phantomjs/issues/12410

Upvotes: 1

Jakozaur
Jakozaur

Reputation: 1977

Works for me. Can you post your PhantomJS version and platform?

Mine on Mac OS X:

$ phantomjs -v
1.9.8

Just works:

$ phantomjs test_phantomjs.coffee 
hello phantomjs

Upvotes: 0

Related Questions