node.js - fs.readDirSync 在打字稿中找不到目录?
问题描述
这是一个非常简单的,我希望从 js 工作:
import * as fs from 'fs';
var testDir = './tests';
// Add each .js file to the mocha instance
fs.readdirSync(testDir)
.filter(function(file) {
// Only keep the .js files
return file.substr(-3) === '.js';
})
.forEach(function(file) {
mochaInstance.addFile(path.join(testDir, file));
});
我正在运行此代码的转译版本,但这会引发错误:
错误:ENOENT:没有这样的文件或目录,在 Object.readdirSync (fs.js:783:3) 处的 scandir './tests'
我该如何解决?我确定文件 './tests' 的目录是正确的,我的节点版本是 v10.13.0
解决方案
您必须使用 __dirname ,它是 Node.js 中的全局变量,用于获取应用程序的当前目录。结合路径,你最终可能会使用这样的东西:
mochaInstance.addFile(path.join(__dirname, testDir, file));
推荐阅读
- javascript - 淡入元素正在移动和调整
- swift - 同一应用程序中的两个观察者相互干扰
- mysql - wordpress 提交审查而不是发布
- r - 使用for循环R创建一个变量时间组合的列表
- javascript - 如何调整lightslider容器的大小
- asp.net - 如何通过来自 .net 的 API 自动登录 Laravel?
- arrays - CGImageDestinationAddImage 的意外行为取决于我在 Swift 中的 UIImage var
- bazel - BUILD 文件可以具有任意文件扩展名吗?
- ajax - Django / Ajax / 数据表
- outlook-2010 - Outlook 任务中的自定义表单未同步