1

I am currently trying to get into NodeJS together with the SailsJS framework. And I want to use coffeescript on the serverside aswell, but after converting all files in config/*.js to config/*.coffee properly with js2coffee, I get the following error when trying to start up the app:

config/400.coffee

SyntaxError: Unexpected token ILLEGAL

pointing to the first character in the file, which is a hashtag for a coffee comment. So it seems the app does not recognize the file as coffeescript, but searching for standard js instead.

I tried installing the package coffee-script and requireing it in

app.js

require('coffee-script');
require('sails').lift(require('optimist').argv);

but it doesn't help.

If I delete 400.coffee, the error appears in the next file 403.coffee etc.

What am I doing wrong? Isn't coffeescript allowed in the config files or am I missing something?

Miiller
  • 1,063
  • 1
  • 10
  • 29
  • How are you calling it from the command line? – Bergi Mar 30 '14 at 20:15
  • @Bergi I am starting up with `sails lift` – Miiller Mar 30 '14 at 20:17
  • I fear `require('coffee-script')` doesn't do what you want. Usually you'd need to compile the coffeescript explicitly to js before executing it with `node`, or use `coffee` to start it up. With sails however [it looks like you need some special flags to the executable](https://github.com/balderdashy/sails/pull/1118). – Bergi Mar 30 '14 at 20:25
  • 2
    If you are using the current stable coffeescript, I believe you need `require('coffee-script/register')`? – loganfsmyth Mar 30 '14 at 20:26
  • @loganfsmyth `require('coffee-script')` to `require('coffee-script/register')` works. Will I run into any problems if I inject it in `app.js`, e.g. performance wise? – Miiller Mar 30 '14 at 20:41
  • possible duplicate of [How to properly set up Coffeescript with Node.js](http://stackoverflow.com/questions/21583899/how-to-properly-set-up-coffeescript-with-node-js) – Aaron Dufour Mar 31 '14 at 17:19

1 Answers1

5

Coffeescript 1.7.0, released Jan 28th, 2014, changed the require('coffee-script') behavior to only load the compiler itself. Now, do load the automatic compiler for .coffee files, you must call require('coffee-script/register') before loading any coffeescript files.

Keep in mind that using this will mean that every .coffee file will be recompiled every time you start node, which could increase startup times. That may or may not be important to you though.

loganfsmyth
  • 156,129
  • 30
  • 331
  • 251
  • I had to change the line in `sails.js` in the sails node module aswell, otherwise the server starts up but other sails commands end up in the same error. Sails itself does n ot seem to be updated for the newer coffee versions. – Miiller Mar 30 '14 at 21:18
  • 1
    If you want to avoid the recompiling issue you mention in the second paragraph, check out [coffee-cache](https://www.npmjs.org/package/coffee-cache). – Aaron Dufour Mar 31 '14 at 02:51