0

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...

Matt Welander
  • 8,234
  • 24
  • 88
  • 138
  • Add console.log('path:', __dirname + '/lib/helper'); before the require to see full path. The __dirname is path to directory the script is in. So in your app.js __dirname should be /var/www/wsApp. I hope this helps you understand how require works. – Molda Mar 14 '16 at 11:49
  • Is a single dot equal to writing __dirname? – Matt Welander Mar 15 '16 at 10:21
  • Since dot means *relative to current directory* and __dirname points to it then yes – Molda Mar 15 '16 at 10:27
  • What is actually *./lib/dbHelper* is it file or directory? If dir then this assumes there's index.js in there if file then you should use dbHelper.js and as pointed out in bellow answer I would recommend using lower case only to avoid any problems with case sensitivity. E.g. `db-helper.js` – Molda Mar 15 '16 at 10:42

1 Answers1

0

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.

Peter Lyons
  • 142,938
  • 30
  • 279
  • 274