【发布时间】:2021-01-08 23:21:54
【问题描述】:
我必须从 Firebase 存储桶中导入一个打字稿文件。它包含数据和类型定义。
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 抱怨:
require 语句不属于 import statement.eslint@typescript-eslint/no-var-requires
开启:
const data = require(tempFilePath)
导入文件的正确方法是什么?
谢谢
【问题讨论】:
标签: typescript firebase google-cloud-functions firebase-storage eslint