我在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