Dynamically generate Express routes fails in production environment

I've seen a lot of folks dynaimcally generating all their routes in the thier routes/index.js like so:

require("fs").readdirSync("./routes", 'utf8').forEach(function(file) {
    if (file != 'index.js' && file != '.DS_Store') {
        require("./"+file);
    }
});

This works fine in development but not in production. If I remove this and add the routes manually it works fine. Any Ideas?

Here's my error if you think that would help:

node.js:134

throw e; // process.nextTick error, or 'error' event on first tick

Error: ENOENT, No such file or directory './routes'
    at Object.readdirSync (fs.js:376:18)
    at Object.<anonymous> (/app/routes/index.js:4:15)
    at Module._compile (module.js:402:26)
    at Object..js (module.js:408:10)
    at Module.load (module.js:334:31)
    at Function._load (module.js:293:12)
    at require (module.js:346:19)
    at Object.<anonymous> (/app/server.js:50:14)
    at Module._compile (module.js:402:26)
    at Object..js (module.js:408:10)
Process died with exit code 1. Restarting...

It seems likely that in production, the current directory is not getting set to the parent of your "routes" directory. How are you launching your app in production? What output do you get from

console.log(process.cwd());

As Mark Bessey says in his answer, you are resolving the routes directory from your current directory -- not relative to where your main script lives. You should probably use __dirname. From the docs:

The name of the directory that the currently executing script resides in.

fs.readdirSync(path.join(__dirname, "routes"))

Also, you don't need to pass 'utf8'. Also, be very careful using any Sync functions in your code -- generally, it's ok in the top level scope, before the server starts accepting requests, so it should be ok in this case.