问题描述
我必须从 Firebase 存储桶中导入一个打字稿文件.它包含数据和类型定义.
I have to import a typescript file from the Firebase storage bucket. It contains data as well as type definitions.
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)
导入文件的正确方法是什么?
What would be the right way to import the file?
谢谢
推荐答案
eslint报错信息中包含错误码typescript-eslint/no-var-requires".对该字符串进行网络搜索会出现 文档:
The eslint error message contains the error code "typescript-eslint/no-var-requires". Doing a web search for that string comes up with the documentation:
除了 import 语句外,不允许使用 require 语句(无变量要求)
换句话说,var foo = require(foo")
等形式的使用是禁止.而是使用 ES6 样式导入或 import foo = require("foo")
进口.
In other words, the use of forms such as var foo = require("foo")
arebanned. Instead use ES6 style imports or import foo = require("foo")
imports.
您将不得不放松您的 eslint 规则以避免出现此消息(不推荐),或者接受它的建议并更改代码的语法以使用 import
.
You will have to either relax your eslint rules to avoid this message (not recommended), or accept its suggestion and change the syntax of your code to use import
.
如果您使用的是 TypeScript,则应阅读 动态导入 可用于 import()
函数.
If you're using TypeScript, you should read the documentation for dynamic imports available with the import()
function.
这篇关于要求语句不是导入语句的一部分.eslint@typescript-eslint/no-var-需要从存储中导入打字稿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!