2017-02-06 61 views
0
路徑

的路徑下正常工作在我的Mac:的JavaScript /節點:在Ubuntu

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

然而,當我的代碼推到Ubuntu和運行node app.js,我得到以下錯誤:

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) 

我應該如何設置路徑,使其在Mac和Ubuntu服務器上都能正常工作?

回答

1

您應該使用與操作系統無關的path.resolve函數,而不是手動連接路徑。這將導致跨操作系統的一致行爲。

var path = require('path'); 

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