Skip to content

Resolving 'Cannot find module' Error in TypeScript

Problem

When using the ts-transformer-keys package in a TypeScript project, you might encounter the error:

Cannot find module 'ts-transformer-keys'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?ts(2792)

This occurs even after properly installing the dependency and adding the transformer configuration to your tsconfig.json file:

json
"plugins": [
    { "transform": "ts-transformer-keys/transformer" }
]

Solution

The error occurs because TypeScript uses the classic module resolution strategy by default, which doesn't automatically search through node_modules directories. You need to explicitly configure TypeScript to use Node.js-style module resolution.

Fix 1: Update tsconfig.json

Add moduleResolution to your tsconfig.json compiler options:

json
{
  "compilerOptions": {
    // ... other options
    "moduleResolution": "node16"
  }
}

INFO

For earlier TypeScript versions (before 4.7), you can use "node" instead of "node16" as the module resolution strategy.

Fix 2: Also for JavaScript Projects

If you're working with a JavaScript project using TypeScript tools, create a jsconfig.json file at your project root with similar configuration:

json
{
  "compilerOptions": {
    "moduleResolution": "node16"
  }
}

Fix 3: Restart Your IDE

After making these changes, restart your code editor (VS Code, WebStorm, etc.) to ensure the changes are properly recognized by the TypeScript language server.

Understanding Module Resolution

TypeScript offers different module resolution strategies:

  • Classic: The original strategy (now deprecated)
  • Node: Mimics Node.js's module resolution
  • Node16/Bundler: Modern strategies for current environments

The Node resolution strategy searches for modules in:

  1. Current directory's node_modules
  2. Parent directories' node_modules
  3. Global npm directories

Complete Configuration Example

Here's a complete tsconfig.json example for using ts-transformer-keys:

json
{
  "compilerOptions": {
    "target": "ES2020",
    "module": "CommonJS",
    "moduleResolution": "node16",
    "strict": true,
    "esModuleInterop": true
  },
  "plugins": [
    { "transform": "ts-transformer-keys/transformer" }
  ]
}

Verification

After applying the fix, import the module normally:

typescript
import { keys } from 'ts-transformer-keys';

interface User {
  id: number;
  name: string;
  email: string;
}

// Should work without errors
const userKeys = keys<User>();
console.log(userKeys); // ['id', 'name', 'email']

WARNING

Ensure you're using a compatible TypeScript version. ts-transformer-keys may have specific version requirements.

Alternative Solutions

If the above doesn't resolve your issue, consider:

  1. Check module path aliases: Verify you haven't misconfigured path mappings
  2. Clear cache: Delete node_modules and reinstall dependencies
  3. Update TypeScript: Ensure you're using a supported TypeScript version

By properly configuring module resolution, TypeScript will correctly locate packages in your node_modules directory, resolving the "Cannot find module" error.