简体   繁体   中英

Can't find module TS2307 in React

import React from "react";
import logo from "./logo.svg";
import text from "./compiler/test/index.txt";
import "./App.css";

const App: React.FC = () => {
  // const compiler = new Compiler("../test/index.jack");
  // compiler.save();
  console.log("text: ", text);

  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.tsx</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
      </header>
    </div>
  );
};

export default App;

I'm using react-app-rewired to override webpack configuration, without ejecting. This component reads a.txt file and logs its content, but there is an error, Cannot find module './compiler/test/index.txt'. TS2307 Cannot find module './compiler/test/index.txt'. TS2307 in the third line of import.

As my comment seemed to resolve the issue, here as an official answer:

You probably just need to declare the type .txt somewhere in your typescript definitions. For create-react-app, this would be in react-app-env.d.ts . Eg:

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

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