πŸ”§ Error Fixes

Cannot Find Module β€” How to Fix in TypeScript and Node.js


Cannot find module 'express' or its corresponding type declarations.
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/app/src/utils'

This error has several different causes depending on whether you’re in TypeScript, Node.js, or both.

Fix 1: Package Not Installed

# Install the package
npm install express

# For TypeScript, you might also need types
npm install -D @types/express

Check if it’s in your package.json. If you cloned a repo, run npm install first.

Fix 2: Missing File Extension (Node.js ESM)

Node.js with ES modules requires file extensions in imports:

// ❌ Node ESM won't resolve this
import { helper } from './utils';

// βœ… Add the extension
import { helper } from './utils.js';

Yes, even for TypeScript files β€” you import .js even though the source is .ts. TypeScript compiles .ts β†’ .js, so the import needs to match the output.

Fix 3: TypeScript Path Issues

// tsconfig.json β€” check these settings
{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"]
    },
    "moduleResolution": "bundler",  // or "node16" for Node.js
    "module": "ESNext"
  }
}

If you’re using path aliases (@/utils), make sure both TypeScript AND your bundler/runtime know about them.

Fix 4: Missing Type Declarations

Could not find a declaration file for module 'some-package'.
# Try installing types
npm install -D @types/some-package

If types don’t exist, create a declaration file:

// src/types/some-package.d.ts
declare module 'some-package';

Fix 5: Wrong moduleResolution

// tsconfig.json
{
  "compilerOptions": {
    // For bundlers (Vite, Webpack, Next.js):
    "moduleResolution": "bundler",

    // For Node.js:
    "moduleResolution": "node16",

    // ❌ Avoid "node" (legacy) for new projects
  }
}

Fix 6: node_modules Corrupted

Nuclear option β€” delete and reinstall:

rm -rf node_modules package-lock.json
npm install

Quick Diagnosis

# Check if the package exists
ls node_modules/express

# Check TypeScript can find it
npx tsc --noEmit

# Check Node.js can find it
node -e "require('express')"