NodeJS-将相对路径转换为绝对路径 [英] NodeJS - convert relative path to absolute

查看:154
本文介绍了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')

这篇关于NodeJS-将相对路径转换为绝对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆