Depcheck is a widely used command-line interface (CLI) tool and programmatic library for analyzing Node.js project dependencies. Its primary function is to identify unused dependencies, detect missing dependencies required by the codebase, and provide insights into how declared dependencies are actually utilized. The current stable version is 1.4.7. While not adhering to a strict release schedule, the project is actively maintained, with multiple patch releases in 2023 and 2021, and significant updates including a breaking change in 2020 (v1.3.x). A key differentiator for Depcheck is its extensive syntax support, covering not only standard JavaScript (ES5-ES7) and React JSX but also CoffeeScript, TypeScript, SASS/SCSS, and Vue.js. Furthermore, it includes 'special' components designed to recognize dependencies used within various configuration files for tools like Babel, ESLint, Webpack, Jest, and Serverless, moving beyond basic `import` or `require` statements. This comprehensive analysis helps developers maintain leaner, more efficient codebases by facilitating the removal of unnecessary packages, which can improve performance and reduce bundle sizes.
npm install depcheckVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to programmatically execute `depcheck` on a project directory, capturing and logging its findings regarding unused, missing, and used dependencies in JSON format. It highlights common configuration options for ignoring specific files or directories and provides a basic structure for handling results.
Update your `.depcheckrc` or programmatic `options.parsers` configuration to use `**/*.js` (or similar) instead of `*.js` for glob patterns.
Run `npm install --save-dev typescript @vue/compiler-sfc` (or `yarn add -D typescript @vue/compiler-sfc`) in your project root to ensure these peer dependencies are available to `depcheck`.
Utilize the `--ignores` CLI option or the `ignore` property in a `.depcheckrc` configuration file to explicitly tell `depcheck` to disregard specific packages that are validly used but misidentified as unused. For directories, use `--ignore-patterns`.
Ensure your project's Node.js environment is version 10 or newer. Use a Node.js version manager like `nvm` to switch to a compatible version.
Consider running `depcheck` within each sub-project directory or configure it to `--ignore-patterns` that contain other `package.json` files. Tools like `lerna` or `nx` often provide mechanisms to run scripts like `depcheck` across individual packages.
Verify that your `tsconfig.json` `paths` and `package.json` `imports` configurations are standard and well-formed. If issues persist, consider adding problematic paths or modules to the `--ignores` or `ignorePatterns` configuration.
For ESM, use `import depcheck from 'depcheck';`. For CommonJS, use `const depcheck = require('depcheck');`. Ensure your `tsconfig.json` `esModuleInterop` is enabled for TypeScript.Install the missing package(s) using `npm install --save-dev typescript @vue/compiler-sfc` (or `yarn add -D typescript @vue/compiler-sfc`).
Add the reported package to the `--ignores` CLI option or the `ignore` array in a `.depcheckrc` configuration file. You can also explicitly enable or configure `specials` via `options.specials` in programmatic use or `parsers` option.
Upgrade `depcheck` to the latest version (`npm install depcheck@latest`) as many parsing issues, including those related to ESM config files and newer TypeScript syntax, have been resolved in recent releases (e.g., `v1.4.3`, `v1.4.4`).
Use the `--ignore-patterns` CLI option or `ignorePatterns` in a `.depcheckrc` file to exclude large directories (e.g., `dist`, `build`, `generated`) and file types (e.g., `*.log`, `*.tmp`) from the analysis. Ensure `node_modules` is implicitly ignored (which it usually is by default).