如何在打字稿中导入 fs?

How to import fs in typesrcipt?

我正在尝试使用以下代码读取 .env.json 文件中的 api 键:

const functionConfig = () => {
  const fs = require("fs");
  return JSON.parse(fs.readFileSync(".env.json"));
};

但我收到以下错误:

Require 语句不是导入语句的一部分@typescript-eslint/no-var-requires

我是 typescript 的新手,所以我们将不胜感激。 :)

这是一个 Typescript lint,可以找到其详细信息on the repository

Disallows the use of require statements except in import statements (no-var-requires)

具体来说,他们提出以下建议

Examples of incorrect code for this rule:

var foo = require('foo');
const foo = require('foo');
let foo = require('foo');

Examples of correct code for this rule:

import foo = require('foo');
require('foo');
import foo from 'foo';

Typescript 通常更喜欢您使用 ES6 风格的模块导入。如果你不能这样做,那么你可以发表评论

/* eslint-disable no-var-requires */