
Product
Socket for Jira Is Now Available
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.
clean-modules
Advanced tools
Clean up/prune unnecessary files and reduce the size of your `node_modules` directory. Useful for CI caches or for reducing the size of serverless functions.
Clean up/prune unnecessary files and reduce the size of your
node_modulesdirectory. Useful for CI caches or for reducing the size of serverless functions.
Simply run the command in the directory where your node_modules are:
clean-modules
You can also pass any options that you need, like custom globs or a path to a specific
node_modules directory.
clean-modules --directory "path/to/node_modules" "extra/included/file/glob.txt" "!extra/excluded/glob.ts
Check out the command section for all available options.
clean-modules can be installed globally if you only want to use it as a CLI tool. You can also
install it locally if you want to use it in a package command.
# global
npm install --global clean-modules
# dev dependency
npm install --save-dev clean-modules
clean-modules clean (default command) 🧹The default command, cleans up your node_modules based on a set of most likely safe glob
patterns and removes empty directories.
clean-modules [options] <...globs>
Extra glob patterns can be passed as positional arguments. By default they are combined with the globs loaded from the the default globs file and any custom globs file passed through the --glob-file option.
For information on how the globs are parsed, see the Glob patterns section.
Example:
# include all TypeScript declaration files and @types folders
clean-modules "**/*.d.ts" "**/@types/**"
# exclude all sourcemap files and PNG files
clean-modules "!**/*.map.js" "!**/*.png"
# include .d.ts files but exclude PNG files
clean-modules "**/*.d.ts" "!**/*.png"
--directory | -Dstring [default: ./node_modules]
Accepts a path to a directory to run the script on.
Example:
clean-modules --directory "path/to/custom/node_modules"
--glob-file | -fstring [default: ./.cleanmodules]
Accepts a path to a file from which clean-modules should read any custom globs. See the glob patterns section for information about how the glob file works and what patterns work.
--no-defaults | -nboolean
Excludes all default globs, using only custom globs added through the glob file or by the include or exclude options. Useful if you want complete control over what files to include.
See the .cleanmodules-default to see what patterns are included by
default by default.
--keep-empty | -kboolean
Skips removing empty folders after removing files.
--dry-run | -dboolean
Runs the script and prints the result without actually removing any files. Does not count the number of removed empty directories.
--json | -jboolean
Only logs a final JSON dump at the end of the script (useful for logs or services).
--yes | -yboolean
Skips the confirmation prompt at the start of the script.
clean-modules analyze 🔎Compiles a list of all files that would be included by clean-modules and gives a breakdown of:
picomatchclean-modules analyze [options]
Because of the amount of data it can be useful to pipe it somewhere, like:
clean-modules analyze >> clean-modules-result.json
The analyze command accepts the same type of positionals as the default command.
The analyze command accepts several of the default command's options and applies them in the same
way:
[
{
"filePath": "/Users/me/projects/foo/node_modules/dependency/__tests__/test1.js",
"includedByDefault": true,
"includedByGlobs": [
{
"original": "__tests__/",
"derived": "/Users/me/projects/foo/node_modules/dependency/**/__tests__/**"
}
]
}
// ...
]
clean-modules accepts globs from either a configuration file (.cleanmodules next to
node_modules by default) or CLI arguments. It uses
.gitignore-like glob patterns
that are converted to valid picomatch globs, which is
what is used to match file paths under the hood.
Differences from regular gitignore syntax:
/, /* or /**
Like with .gitignore, globs should use forward-slashes as separators on all operative systems (including Windows)!
# this is a comment (starts with a #)
# to include include directories, end patterns with / or /* or /**
dep1/
dep1/*
dep2/**
# files are matched in any directory by default
**/*.test.js
# is the same as
*.test.js
# use a leading / to include a file or directory at a specific place
/dep4/this/specific/directory/**
/dep4/this/specific/file.js
# to exclude a path, prepend it with a !
!/not/this/directory/
!not-me.js
# to use leading exclamation marks without excluding, escape them
\!(*.d).ts
The default globs can be found in the .cleanmodules-default file. It
only contains inclusions (as exclusions would override custom inclusions) and consists of a large
list of the most common files that are safe to remove.
That said, it's impossible to guarantee that none of the files are needed, and you might need to do custom exclusions depending on what packages you use.
**/*.d.ts: If you don't use TypeScript. TypeScript declaration files take up a lot of space
in your node_modules folder, but they are most likely required to build your application. Useful
locally even if you don't use TypeScript since they can be parsed by your IDE.!**/*.map.js: If you are running clean-modules locally or need source files in production.
clean-modules removes sourcemap files by default since they take up a lot of space and does not
break builds when removed. They can be nice to have though, especially while developing.Clean modules can be used programmatically too!
Simply import the corresponding function from the package:
import { clean, analyze } from 'clean-modules';
// analyze, all options are optional
const analyzeResult = await analyze({
directory: '/path/to/node_modules',
globFile: '/path/to/.cleanmodules',
globs: ['**/*.js'],
noDefaults: false,
});
// clean, all options are optional
const cleanResult = await clean({
directory: '/path/to/node_modules',
globFile: '/path/to/.cleanmodules',
globs: ['**/*.js'],
noDefaults: false,
keepEmpty: false,
dryRun: false,
});
The most common issues I found with available tools are:
@types/react-native.*.ts by default, which breaks TypeScript declaration files on build.yarn clean.d.ts files and assets folder)filepath.Match.d.ts files and assets folder)FAQs
Clean up/prune unnecessary files and reduce the size of your `node_modules` directory. Useful for CI caches or for reducing the size of serverless functions.
The npm package clean-modules receives a total of 49,899 weekly downloads. As such, clean-modules popularity was classified as popular.
We found that clean-modules demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.