使用ts-node导入文本文件



我想导入一个文本文件:

import text from "./text.txt";

我有一个文件:

declare module "*.txt" {
const content: string;
export default content;
}

然而,ts-node抱怨:

error TS2307: Cannot find module './text.txt' or its corresponding type declarations.

请注意,我不能使用fs模块读取文件,因为我也为web前端使用相同的代码(其中导入由捆绑器解析)。

编辑尝试在第一个答案中使用提议:

import text from "./text.txt!text";

declare module "*!text" {
const content: string;
export default content;
}

我得到这个错误:

Cannot find module './text.txt!text' from '...'

你必须告诉typescript你的模块将是这样的字符串类型:从"./text.txt中导入文本

最新更新