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.
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.
The primary solution is to install the type definitions for lodash:
# Using npm
npm install --save-dev @types/lodash
# Using yarn
yarn add --dev @types/lodash
# Using pnpm
pnpm add -D @types/lodashAfter installation, TypeScript will automatically detect the types:
// 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"
Check that @types/lodash was installed correctly:
# 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:
└── @types/[email protected]If you're using a monorepo with workspaces, ensure @types/lodash is installed in the correct workspace:
# 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/lodashEnsure your tsconfig.json is configured to find type definitions:
{
"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:
{
"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:
{
"compilerOptions": {
// WRONG - this excludes lodash types
"types": ["node", "jest"]
// CORRECT - include lodash explicitly if using types field
"types": ["node", "jest", "lodash"]
}
}If you're using lodash-es (the ES module version) instead of lodash, install its specific types:
npm install --save-dev @types/lodash-esUpdate your imports to use the ES module version:
// 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-esNote: 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.
If types still aren't recognized, clear your dependency cache:
# 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 installAfter reinstalling, restart your development server and IDE:
# 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.
If you cannot install @types/lodash immediately, create a temporary declaration file:
// 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:
{
"include": [
"src/**/*",
"types/**/*"
]
}Warning: This is a workaround. You lose all the benefits of proper lodash types. Install @types/lodash as soon as possible.
### 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:
{
"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:
// 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):
{
"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:
// 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:
// Without @types/lodash
import _ from "lodash"; // _ has type 'any'
const result = _.map([1, 2], x => x * 2); // result: any, x: anyInstalling @types/lodash resolves this:
// 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:
# 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'
doneInstall all needed @types packages at once:
npm install --save-dev @types/lodash @types/express @types/nodeFunction expression requires a return type
Function expression requires a return type
Value of type 'string | undefined' is not iterable
How to fix "Value is not iterable" in TypeScript
Type 'undefined' is not assignable to type 'string'
How to fix "Type undefined is not assignable to type string" in TypeScript
Type narrowing from typeof check produces 'never'
How to fix "Type narrowing produces never" in TypeScript
Type parameter 'T' has conflicting constraints
How to fix "Type parameter has conflicting constraints" in TypeScript