Search code examples
node.jspathbase-path

problems with node paths in prod env


I have my first node server app running fine on localhost, but in a digitalocean (ubuntu) environment I can't get the paths to work.

This line

require("./lib/dbHelper").initilize();

Generate this error

Error: Cannot find module './lib/dbHelper'
at Function.Module._resolveFilename (module.js:339:15)
at Function.Module._load (module.js:290:25)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at Object.<anonymous> (/var/www/wsApp/app.js:18:1)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)

And this line

let User = require("../models/User");

generates this error

Error: Cannot find module '../models/User'
at Function.Module._resolveFilename (module.js:339:15)
at Function.Module._load (module.js:290:25)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at Object.<anonymous> (/var/www/wsApp/routes/auth.js:5:12)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)

I have looked here (https://gist.github.com/branneman/8048520) for solutions and I have tried suggestions 3, 4.2 and 6 but nothing changes the error messages.

Worth noting is that this line seems to work fine

app.use("/", require("./routes/auth.js"));

Being new to node I don't quite understand the path mappings, and it seems strange that this last one would work while the first one doesn't since they both use the dot syntax...


Solution

  • If you are developing on a case-insensitive filesystem (OS X or Windows), you may be getting tripped up by the fact that the linux filesystem is case sensitive. Check, for example, that the "H" in "dbHelper" is really uppercase in your git repository.