要求语句不是导入的一部分statement.eslint@typescript-eslint/没有var需要从存储中导入



我必须从Firebase存储桶中导入一个typescript文件。它包含数据和类型定义。

export interface MyData {
// ...
}
export const myData = {
// ...
}

这是我附带的代码:

export const readData = async (fileDir: string, fileName: string): Promise<object> => {
// Open the bucket
const bucket: Bucket = admin.storage().bucket()
// Define the file path in the bucket
const filePath: string = path.join(fileDir, fileName)
// Define the local file path on the cloud function's server
const tempFilePath: string = path.join(os.tmpdir(), fileName)
// Download the file from the bucket
try {
await bucket.file(filePath).download({ destination: tempFilePath })
} catch (error) {
functions.logger.error(error, { structuredData: true })
}
// Extract the needed variable export from the file
const data = require(tempFilePath)
// provide the variable
return data
}

eslint抱怨:

要求语句不是导入的一部分statement.eslint@typescript-eslint/无var需要

打开:

const data = require(tempFilePath)

导入文件的正确方式是什么?

感谢

esint错误消息包含错误代码"typescript eslint/no var需要"。在网络上搜索该字符串会得到文档:

除导入语句外,不允许使用require语句(无需var(

换句话说,诸如var foo = require("foo")之类的形式的使用是被禁止。而是使用ES6样式导入或import foo = require("foo")进口。

您必须放宽esint规则以避免此消息(不推荐(,或者接受其建议并更改代码语法以使用import

如果您使用的是TypeScript,您应该阅读import()函数提供的动态导入文档。

最新更新