NodeJS-将相对路径转换为绝对路径

问题描述:

在我的文件系统中,我的工作目录位于以下位置:

In my File-system my working directory is here:

C:\ temp \ a \ b \ c \ d

在b \ bb下有文件:tmp.txt

and under b\bb there's file: tmp.txt

C:\ temp \ a \ b \ bb \ tmp.txt

如果要从工作目录转到该文件,将使用以下路径:

If I want to go to this file from my working directory, I'll use this path:

"../../bb/tmp.txt"

如果文件不存在,我想记录完整路径并告诉用户:
文件C:\ temp \ a \ b \ bb \ tmp.txt不存在" .

In case the file is not exist I want to log the full path and tell the user:
"The file C:\temp\a\b\bb\tmp.txt is not exist".

我的问题:

我需要一些函数,可以将相对路径:"../../bb/tmp.txt"转换为绝对路径:"C:\ temp \ a\ b \ bb \ tmp.txt"

I need some function that convert the relative path: "../../bb/tmp.txt" to absolute: "C:\temp\a\b\bb\tmp.txt"

在我的代码中,它应该是这样的:

In my code it should be like this:

console.log("The file" + convertToAbs("../../bb/tmp.txt") + " is not exist")

使用 path.解决

尝试:

resolve = require('path').resolve
resolve('../../bb/tmp.txt')