5

I'm trying to define some Node routes using Coffeescript in the following way:

My app.js file:

/**
 * Module dependencies.
 */

require('coffee-script');

var express = require('express');


var http = require('http');

var path = require('path');

var app = express();

// all environments

app.set('port', process.env.PORT || 3000);

app.set('views', path.join(__dirname, 'views'));

app.set('view engine', 'jade');

app.use(express.favicon());

app.use(express.logger('dev'));

app.use(express.json());

app.use(express.urlencoded());

app.use(express.methodOverride());

app.use(app.router);

app.use(express.static(path.join(__dirname, 'public')));

// development only

if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

require('./routes')(app);

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

My routes.coffee file (located at the same level as app.'s):

routes = (app) ->


app.get '/login', (req,res) ->

    res.render "views/login",

        title: 'Login'

        stylesheet: 'login'

module.exports = routes

My issue are:

1 when I try to run node app - I get:

module.js:340
    throw err;
      ^
Error: Cannot find module './routes'

If I explicitly specify the .coffee suffix: require('./routes.coffee')(app); - I get:

routes = (app) ->
                ^
SyntaxError: Unexpected token >

What is the proper way of doing this, please?

5
  • Do you want to mix Coffee and Javascript on the same project or is Coffee your main language? Commented Feb 5, 2014 at 17:40
  • What version of CoffeeScript are you using? Specifically, things changed in 1.7 with regard to the way you're trying to use it. Commented Feb 5, 2014 at 18:12
  • The version os node is: v0.10.25 And the version of Coffee is: CoffeeScript version 1.7.1 Commented Feb 5, 2014 at 18:16
  • No, the version of CoffeeScript. In a node REPL, you can do require('coffee-script').VERSION. Commented Feb 5, 2014 at 18:18
  • I would prefer to just use Coffeescript throughout the entire back-end of the application, if possible Commented Feb 5, 2014 at 18:19

1 Answer 1

15

In CoffeeScript 1.7, the line require('coffee-script') no longer allows you to require CoffeeScript files. Instead, you need to either:

require('coffee-script/register')

or

require('coffee-script').register()

See the documentation.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! using /register did the trick. I will now open a different question, since my newly recognized routes.coffee now triggers ˚ ReferenceError: app is not defined

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.