1

I'm getting the following error :

/Users/nblavoie/Desktop/HotPie/HotPie/apps/authentication/routes.coffee:6
  app.get('/login', function(req, res) {
  ^
ReferenceError: app is not defined
    at Object.<anonymous> (/Users/nblavoie/Desktop/HotPie/HotPie/apps/authentication/routes.coffee:6:3)
    at Object.<anonymous> (/Users/nblavoie/Desktop/HotPie/HotPie/apps/authentication/routes.coffee:15:4)
    at Module._compile (module.js:449:26)
    at Object.require.extensions..coffee (/Users/nblavoie/Desktop/HotPie/HotPie/node_modules/coffee-script/lib/coffee-script/coffee-script.js:22:21)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:362:17)
    at require (module.js:378:17)
    at Object.<anonymous> (/Users/nblavoie/Desktop/HotPie/HotPie/app.js:31:1)
    at Module._compile (module.js:449:26)

While trying to pass the (app) variable to the following require :

/**
 * Module dependencies.
 */

require('coffee-script');

var express = require('express')
  , http = require('http')
  , path = require('path');

var app = express();

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(path.join(__dirname, 'public')));
});

app.configure('development', function(){
  app.use(express.errorHandler());
});

// Routes
**require('./apps/authentication/routes')(app);**

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

Using the app variable in the following file :

routes = (app) -> 
app.get '/login', (req, res) ->
    res.render 'views/login',
        title: 'Login'
        stylesheet: 'login'
module.exports = routes

I know that the variable scope is different from one JS file to another. But why does the following line :

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

Doesn't pass the app variable?

3
  • 1
    We're probably going to need to see (some other code here) Commented Aug 24, 2012 at 15:49
  • I updated the code and the stack error. Commented Aug 24, 2012 at 15:56
  • I found a possible solution. By removing the var infront of the app variable, I'm not getting the error anymore. Is it because I'm changing the variable scope? app = express(); Commented Aug 24, 2012 at 15:58

2 Answers 2

3

Right here:

ReferenceError: app is not defined
    at Object.<anonymous> (/Users/nblavoie/Desktop/HotPie/HotPie/apps/authentication/routes.coffee:6:3)

The error isn't in app.js, it's in your CoffeeScript file. Check out what it's compiling to:

var routes;

routes = function(app) {};

app.get('/login', function(req, res) {
  return res.render('views/login', {
    title: 'Login',
    stylesheet: 'login'
  });
});

module.exports = routes;

Notice that routes is an empty function? Yep, you missed an indent. Changing your CoffeeScript to the following should fix it (notice the indentation of app.get and everything after it):

routes = (app) ->
    app.get '/login', (req, res) ->
        res.render 'views/login',
            title: 'Login',
            stylesheet: 'login'

module.exports = routes
Sign up to request clarification or add additional context in comments.

Comments

2

CoffeeScript is very sensitive to indentation. This code should work:

routes = (app) -> 
    app.get '/login', (req, res) ->
        res.render 'views/login',
            title: 'Login'
            stylesheet: 'login'
module.exports = routes

1 Comment

This isn't actually correct. The module.export... line is indented, meaning that it'll be compiled within the routes function. That means that the exports line won't actually get run. It needs to be unindented.

Your Answer

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