小编典典

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

node.js

在我的 文件系统中, 我的工作目录在这里:

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

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

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

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

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

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

我的问题:

我需要一些 功能转换 的相对路径: “../../bb/tmp.txt”绝对: “C:\ TEMP \ A \ B \ BB \
tmp.txt”

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

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

阅读 481

收藏
2020-07-07

共1个答案

小编典典

path.resolve

尝试:

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