【问题标题】:Require statement not part of import statement.eslint@typescript-eslint/no-var-requires importing typescript from storage要求语句不是导入语句的一部分。eslint@typescript-eslint/no-var-需要从存储中导入打字稿
【发布时间】: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


    【解决方案1】:

    eslint 错误信息包含错误代码“typescript-eslint/no-var-requires”。对该字符串进行网络搜索会出现documentation:

    除了 import 语句外,不允许使用 require 语句 (无变量要求)

    也就是说,var foo = require("foo")等形式的使用是 禁止。而是使用 ES6 样式导入或 import foo = require("foo") 进口。

    您将不得不放松您的 eslint 规则以避免此消息(不推荐),或者接受它的建议并更改您的代码语法以使用 import。

    如果您使用的是 TypeScript,则应阅读 dynamic imports 的文档,该文档可用于 import() 函数。

    【讨论】:

    猜你喜欢
    • 2020-04-04
    • 2020-05-13
    • 1970-01-01
    • 2022-06-17
    • 2019-04-12
    • 2017-08-13
    • 2019-06-29
    • 2016-11-20
    • 2023-03-21
    相关资源
    最近更新 更多