Require语句不是import语句的一部分.eslint @ typescript-eslint/no-var-需要从存储中导入打字稿 [英] Require statement not part of import statement.eslint@typescript-eslint/no-var-requires importing typescript from storage
问题描述
我必须从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语句.eslint@ typescript-eslint/no-var-requires
Require statement not part of 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语句(no-var-requires)
Disallows the use of require statements except in import statements (no-var-requires)
换句话说,使用诸如 var foo = require("foo")
之类的形式是被禁止.而是使用ES6样式导入或 import foo = require("foo")
进口.
In other words, the use of forms such as var foo = require("foo")
are
banned. Instead use ES6 style imports or import foo = require("foo")
imports.
您将必须放松您的陪同规则以避免此消息(不建议),或者接受其建议并更改代码的语法以使用 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
.
If you're using TypeScript, you should read the documentation for dynamic imports available with the import()
function.
这篇关于Require语句不是import语句的一部分.eslint @ typescript-eslint/no-var-需要从存储中导入打字稿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!