Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检查是否存在相对路径_Javascript_File_Path - Fatal编程技术网

Javascript 检查是否存在相对路径

Javascript 检查是否存在相对路径,javascript,file,path,Javascript,File,Path,我在一个文件checkExists.js export const checkIfExists = (input) => { return fs.existsSync(input) } 我有一个测试,test.js,我不能以任何方式编辑它 import {checkIfExists} from '/checkExists' checkIfExists('../../myTestFile.js').shouldBeTrue 。/../myTestFile.js是到test.js的

我在一个文件
checkExists.js

export const checkIfExists = (input) => {
   return fs.existsSync(input)
}
我有一个测试,
test.js
,我不能以任何方式编辑它

import {checkIfExists} from '/checkExists'

checkIfExists('../../myTestFile.js').shouldBeTrue
。/../myTestFile.js
是到
test.js
的正确相对路径,但不是到
checkExists.js


有没有办法解析checkExists.js中的路径,以便它可以支持相对路径?我无法传递其他路径,
checkIfExists
函数必须能够完成所有工作。

您可以使用
路径
模块解析文件的路径

import * as path from 'path';
...

checkIfExists(path.resolve(__dirname, '../../myTestFile.js')).shouldBeTrue

您可能想切换到fs.stat