user1107173
user1107173

Reputation: 10744

Javascript/Node: Path on Ubuntu

The path below works fine on my Mac:

var Auth = require(__dirname + '/../db/models/Auth')

However, when I push up the code to Ubuntu and run node app.js, I get the following error:

Error: Cannot find module '/home/ubuntu/www/middleware/../db/models/Auth'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/home/ubuntu/www/middleware/authenticate.js:1:74)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/home/ubuntu/www/routes/friendR.js:4:24)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)

How should I set the path so it works fine on both, the Mac and Ubuntu server?

Upvotes: 0

Views: 181

Answers (1)

user5224313
user5224313

Reputation:

You should use the OS-independent path.resolve function instead of manually concatenating the paths. This will result in consistent behavior across OSes.

var path = require('path');

var Auth = require(path.resolve(__dirname, '../db/models/Auth'));

Upvotes: 1

Related Questions