Nestjs readFileSync return 无法读取未定义的 属性 'readFileSync'

Nestjs readFileSync return Cannot read property 'readFileSync' of undefined

我正在尝试使用方法 readFileSync:

获取文件
import fs from 'fs';
import path from 'path';

const templateFile = fs.readFileSync(
    path.resolve(
    __dirname,
    '../mail/templates/exampleTemplate.html',
    ),
    'utf-8',
);

巢还在return我的错误:

TypeError: Cannot read property 'readFileSync' of undefined

我试过路径:./templates/exampleTemplate.html,但结果是一样的

我有一个结构文件:

我通过更改修复它:

import fs from 'fs';
import path from 'path';

至:

import fs = require('fs');
import path = require('path');

由于您使用的是 Typescript 并且 fs 没有默认导出,因此您必须使用 import * as fs from 'fs'.

尝试

import {readFileSync} from 'fs'