简体   繁体   中英

Import text file with ts-node

I'd like to import a text file:

import text from "./text.txt";

I have a file with:

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

However, ts-node complains with:

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

Please note that I cannot use the fs module to read the file since I am also using the same code for the web frontend (where the import is resolved by the bundler).

EDIT

Trying to use the proposal in the first answer:

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

with

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

I get this error:

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

you have to tell typescript that your module will be a string type like that: import text from "./text.txt;text";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM