Search code examples
reactjsnode.jstypescript

TypeScript does not recognize import paths


I'm having trouble dealing with the following error. My tech stack includes React, TypeScript, and node.js, and I'm using VSCode on a Mac.

I'm not sure exactly when it happened, but despite the file router.ts definitely existing, I keep getting the error: "Module not found: Error: Can't resolve '../src/router' in '/Users/sample/test/js_taskList/frontend/src'". My directory structure is as follows, and I'm importing router.ts in App.tsx:

root
  └── frontend
       └── src
            ├── router.ts
            └── App.tsx

I've checked the import path and file name for duplicates or errors but found nothing wrong. Here's how I've written it in App.tsx:

import React, { useEffect, useState } from 'react';
import './App.css';
import { fetchData, addData, deleteData, toggleData } from './router';

If there's any information I'm missing or anything you can point out, I'd appreciate it. Thank you for your help.


Solution

  • You probably just need to add the extension (normally required by ES modules):

    import { ... } from './router.ts';
    

    Or you can change TypeScript's module compiler options to something that allows extensionless relative paths.