I am writing a basic React/Express application with TS, as I haven't used it much before and want to learn. I am running into an issue where I attempt to import a component into another component, but I receive a "module not found" error.
It works for imports from node modules so I suspect it's an issue with my TS configuration, but Google and SO haven't had any helpful info yet.
This happens with every component I have defined, not just the Counter component in the example below.
The error message:
[0] ERROR in ./src/App.tsx 5:0-51
[0] Module not found: Error: Can't resolve './components/Counter/Counter' in '/home/name/Git/project/client/src'
[0]
[0] webpack compiled with 1 error
The code in question:
App.tsx
import { Outlet } from "react-router";
import Counter from "./components/Counter/Counter"; // The problem line
const App = () => {
return (
<div>
<Outlet />
<Counter />
</div>
);
};
export default App;
Counter.tsx
function Counter() {
return (
<div>
<button>Increment 1</button>
</div>
);
}
export default Counter;
tsconfig.json
{
"compilerOptions": {
"target": "ESNext",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"ts-node": {
"esm": true,
"experimentalSpecifierResolution": "node"
}
}
Project Setup:
├─ tsconfig.json
├─ (express-related files)
└─ client
├─ public
└─ src
├─ App.tsx
├─ index.tsx
├─ (other react-related files/dirs)
└─ components
└─ Counter
└─ Counter.tsx
I have tried explicitly including directories in the tsconfig.json file, similar to this:
"include": ["./client/src"]
I have a temporary fix by adding the following to react-app-env.d.ts:
declare module "*.tsx";
declare module "*.ts";
With this, I can change my import statements to import Counter from "./components/Counter/Counter.tsx";
(adding the file extension) and it will work. However, this is different from other TS projects I've seen, so I don't think it is correct.