How to fix Could not find a declaration file for module 'lodash' in TypeScript

TypeScriptBEGINNERMEDIUMUpdated Dec 8, 2025· 144 views

This TypeScript error occurs when you import lodash (or similar JavaScript libraries) without the corresponding type definitions installed. The fix is to install the @types/lodash package, which provides TypeScript type declarations for lodash's functions and modules.

Quick fix

Fixes TS7016

# Using npm
npm install --save-dev @types/lodash

# Using yarn
yarn add --dev @types/lodash

# Using pnpm
pnpm add -D @types/lodash
  1. Install @types/lodash package
  2. Verify package installation and location
  3. Check typeRoots configuration in tsconfig.json
  4. Handle lodash-es variant separately
typescript — error
# Using npm
npm install --save-dev @types/lodash
# Using yarn
yarn add --dev @types/lodash
# Using pnpm
pnpm add -D @types/lodash
Could not find a declaration file for module 'lodash'

What this error means

The "Could not find a declaration file for module 'lodash'" error (TS7016) appears when TypeScript cannot find type definitions (.d.ts files) for the lodash module. Lodash is a JavaScript library written before TypeScript became popular, so it doesn't include built-in type definitions in its main package. TypeScript needs type information to provide type checking, autocomplete, and other language features. When you import a JavaScript library without types, TypeScript treats all its exports as having the 'any' type, which defeats the purpose of using TypeScript and can lead to runtime errors. The TypeScript community maintains type definitions for popular JavaScript libraries in the DefinitelyTyped repository. These are published as @types packages on npm. For lodash, you need to install @types/lodash as a development dependency to get full type safety and IDE support. This error commonly occurs after installing lodash but forgetting the types package, or when migrating a JavaScript project to TypeScript.

Typical symptoms

  • Compilation fails with 'Could not find a declaration file for module 'lodash''
  • Error code TS7016 appears in TypeScript compiler output
  • Red underlines appear in IDE on lodash import statements
  • Lodash functions show as type 'any' instead of proper types
  • Autocomplete doesn't work for lodash methods in your editor
  • Project builds successfully but TypeScript shows errors in development

Common causes

  1. The @types/lodash package is not installed in your project
  2. The @types/lodash package is installed but in the wrong location (e.g., wrong workspace in monorepo)
  3. TypeScript compiler cannot find the @types directory (typeRoots misconfigured)
  4. Using lodash-es instead of lodash without matching @types package
  5. Version mismatch between lodash and @types/lodash packages
  6. Types package excluded in tsconfig.json or .gitignore'd node_modules/@types

How to fix "Could not find a declaration file for module 'lodash'"

1Install @types/lodash package

The primary solution is to install the type definitions for lodash:

bash
# Using npm
npm install --save-dev @types/lodash

# Using yarn
yarn add --dev @types/lodash

# Using pnpm
pnpm add -D @types/lodash

After installation, TypeScript will automatically detect the types:

typescript
// This will now have full type support
import _ from "lodash";
import { debounce, groupBy } from "lodash";

const numbers = [1, 2, 3, 4];
const doubled = _.map(numbers, n => n * 2);  // Type: number[]

Restart your IDE or TypeScript server to ensure it picks up the new types:
- VS Code: Press Cmd/Ctrl + Shift + P → "TypeScript: Restart TS Server"

2Verify package installation and location

Check that @types/lodash was installed correctly:

bash
# Verify the package exists
npm list @types/lodash

# Check node_modules structure
ls -la node_modules/@types/lodash

# View package.json to confirm it's listed
cat package.json | grep "@types/lodash"

Expected output should show:

bash
└── @types/lodash@X.Y.Z

If you're using a monorepo with workspaces, ensure @types/lodash is installed in the correct workspace:

bash
# For npm workspaces
npm install --workspace=packages/my-app --save-dev @types/lodash

# For yarn workspaces (install from the specific workspace directory)
cd packages/my-app && yarn add --dev @types/lodash
3Check typeRoots configuration in tsconfig.json

Ensure your tsconfig.json is configured to find type definitions:

json
{
  "compilerOptions": {
    "typeRoots": [
      "./node_modules/@types"
    ],
    "types": [],  // Empty array means include all @types packages
    "moduleResolution": "node",
    "esModuleInterop": true
  }
}

If you've customized typeRoots, make sure it includes the location where @types packages are installed:

json
{
  "compilerOptions": {
    "typeRoots": [
      "./node_modules/@types",
      "./custom-types"  // Any additional custom type directories
    ]
  }
}

The types field can limit which @types packages are loaded. An empty array or omitting it means all are included:

json
{
  "compilerOptions": {
    // WRONG - this excludes lodash types
    "types": ["node", "jest"]

    // CORRECT - include lodash explicitly if using types field
    "types": ["node", "jest", "lodash"]
  }
}
4Handle lodash-es variant separately

If you're using lodash-es (the ES module version) instead of lodash, install its specific types:

bash
npm install --save-dev @types/lodash-es

Update your imports to use the ES module version:

typescript
// For lodash (CommonJS)
import _ from "lodash";
import { debounce } from "lodash";  // Types from @types/lodash

// For lodash-es (ES Modules)
import _ from "lodash-es";
import { debounce } from "lodash-es";  // Types from @types/lodash-es

Note: lodash and lodash-es have different module structures. Make sure your imports match the package you installed. Using the wrong types package will cause module resolution issues.

5Clear cache and reinstall dependencies

If types still aren't recognized, clear your dependency cache:

bash
# Remove node_modules and lock files
rm -rf node_modules
rm package-lock.json  # or yarn.lock / pnpm-lock.yaml

# Clear package manager cache
npm cache clean --force
# or
yarn cache clean
# or
pnpm store prune

# Reinstall all dependencies
npm install

After reinstalling, restart your development server and IDE:

bash
# Restart dev server
npm run dev

# In VS Code, restart TypeScript
# Cmd/Ctrl + Shift + P → "TypeScript: Restart TS Server"

For CI/CD pipelines, ensure @types/lodash is in devDependencies so it gets installed during build.

6Create a custom declaration file (temporary workaround)

If you cannot install @types/lodash immediately, create a temporary declaration file:

typescript
// types/lodash.d.ts
declare module "lodash" {
  const _: any;
  export = _;
}

// Or with basic types:
declare module "lodash" {
  export function map<T, U>(
    collection: T[],
    iteratee: (value: T) => U
  ): U[];

  export function debounce<T extends (...args: any[]) => any>(
    func: T,
    wait?: number
  ): T;

  // Add other functions you use...
}

Include this file in your tsconfig.json:

json
{
  "include": [
    "src/**/*",
    "types/**/*"
  ]
}

Warning: This is a workaround. You lose all the benefits of proper lodash types. Install @types/lodash as soon as possible.

Advanced notes

### Understanding @types Packages

The @types namespace on npm hosts type definitions maintained by the DefinitelyTyped community. When you install @types/lodash, you get:

1. Type definitions: All lodash functions, parameters, and return types
2. JSDoc documentation: Inline documentation in your IDE
3. Autocomplete: Full IntelliSense support for all lodash methods
4. Type safety: Compile-time checking for incorrect usage

You can browse available @types packages at:
- [TypeSearch](https://www.typescriptlang.org/dt/search)
- [DefinitelyTyped Repository](https://github.com/DefinitelyTyped/DefinitelyTyped)

### Version Compatibility

@types packages follow semantic versioning aligned with the main library:

json
{
  "dependencies": {
    "lodash": "^4.17.21"
  },
  "devDependencies": {
    "@types/lodash": "^4.14.200"  // Major version matches
  }
}

TypeScript will warn if there's a significant version mismatch, but minor/patch versions don't need to match exactly.

### Specific Lodash Module Imports

Lodash supports importing individual modules for smaller bundle sizes:

typescript
// Entire lodash library
import _ from "lodash";

// Specific function (CommonJS)
import debounce from "lodash/debounce";

// Specific function (ES Modules with lodash-es)
import { debounce } from "lodash-es";

For individual module imports (e.g., lodash/debounce), @types/lodash includes declarations for all submodules automatically. No additional setup needed.

### Alternative: Use skipLibCheck

If you want to suppress these errors globally (not recommended for production):

json
{
  "compilerOptions": {
    "skipLibCheck": true  // Skips type checking for all .d.ts files
  }
}

Caution: This disables type checking for ALL declaration files, not just lodash. You lose type safety across your entire dependency tree. Only use this as a last resort or in legacy migration scenarios.

### Tree-Shaking with Lodash

When using lodash with modern bundlers, import specific functions to enable tree-shaking:

typescript
// BAD - imports entire lodash library (~70KB)
import _ from "lodash";
_.map([1, 2, 3], x => x * 2);

// GOOD - imports only map function
import map from "lodash/map";
map([1, 2, 3], x => x * 2);

// BEST - use lodash-es with named imports
import { map } from "lodash-es";
map([1, 2, 3], x => x * 2);

The @types packages support all these import styles with correct types.

### Implicit Any Errors

With "strict": true or "noImplicitAny": true, using lodash without types causes cascading 'any' type errors:

typescript
// Without @types/lodash
import _ from "lodash";  // _ has type 'any'

const result = _.map([1, 2], x => x * 2);  // result: any, x: any

Installing @types/lodash resolves this:

typescript
// With @types/lodash
import _ from "lodash";  // _ has proper lodash types

const result = _.map([1, 2], x => x * 2);  // result: number[], x: number

### Migrating from JavaScript

When migrating a JavaScript project to TypeScript, you'll see this error for all untyped dependencies. Create a checklist:

bash
# Find all imports from packages
grep -r "from ['"]" src/ | grep -v node_modules | cut -d"'" -f2 | sort -u

# Check which need @types
for pkg in $(cat package.json | jq -r '.dependencies | keys[]'); do
  npm search @types/$pkg --json | jq -r '.[0].name'
done

Install all needed @types packages at once:

bash
npm install --save-dev @types/lodash @types/express @types/node

Frequently asked questions

What does "TS7016" mean?
ts files) for the lodash module. Lodash is a JavaScript library written before TypeScript became popular, so it doesn't include built-in type definitions in its main package. TypeScript needs type information to provide type checking, autocomplete, and other language features.
How do I fix "TS7016"?
This TypeScript error occurs when you import lodash (or similar JavaScript libraries) without the corresponding type definitions installed. The fix is to install the @types/lodash package, which provides TypeScript type declarations for lodash's functions and modules.
What causes "TS7016"?
The @types/lodash package is not installed in your project The @types/lodash package is installed but in the wrong location (e.g., wrong workspace in monorepo) TypeScript compiler cannot find the @types directory (typeRoots misconfigured)
Is the "TS7016" error serious?
It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.

Official resources & further reading