我在the_second_folder中有nodejs代碼,但我想從根文件夾運行它。當我從the_second_folder運行代碼時,我沒有收到任何錯誤,但是當我從根文件夾nodejs崩潰時,因爲包含而運行代碼。下面是代碼:Nodejs在從相對路徑運行時無法找到包含
require('rootpath')(); //does not work
var file_to_include = "file.js";
eval(fs.readFileSync(file_to_include)+'');
這隻有當我從the_second_folder(它是)上運行的代碼,但無法從根文件夾中運行時的作品(-1文件夾樹)。 錯誤恰恰是對列入file_to_include
的我想知道的是,我怎麼能運行在從根文件夾,名爲即使在相對的文件夾範圍內的節點腳本。
編輯
file_to_include ABSOLUTE_PATH
Error: ENOENT, no such file or directory 'file_to_include.js'
...
at Object.<anonymous> (/root/the_second_folder/socket.io/lib/client.js:3:9)
...
at Object.<anonymous> (/root/the_second_folder/socket.io/lib/index.js:12:14)
現在的錯誤socket.io發生......對此有何想法?
由於相對路徑。 – Scimonster
@Scimonster我知道x)我怎麼能這樣做,所以當從其他文件夾調用時,一切都是相對的? – Fane
但是,你不是在問如何解決這個問題,而是在問爲什麼會發生這種情況,而這聽起來並不是你真正想知道的。我建議你[編輯]問你想要什麼。如果事情很清楚,那我就不會放棄。 – Scimonster