optimize-deps
v0.1.1
Published
Create a minimized node_modules directory from the files used by Node.js entrypoints.
Readme
optimize-deps
Create a smaller node_modules directory for the JavaScript files your Node.js app actually uses.
npx optimize-deps src/main.js src/second.js src/mydiroptimize-deps accepts JavaScript files and directories. Files are verified before tracing. Directories are walked recursively and .js, .mjs, and .cjs files are added as entrypoints, while node_modules, .git, and the output directory are skipped.
The CLI uses @vercel/nft to trace runtime dependencies, then copies only traced root node_modules/* entries into node_modules_optimized.
Each run reports the approximate node_modules size before optimization, the optimized size after tracing, and the estimated savings:
Approx node_modules size: 10.4 MB before -> 1.4 MB after (saved 9.1 MB, 86.9%)Options
optimize-deps [options] <js-file-or-directory...>
--replace Replace node_modules with the optimized output
--exports-only Pass exportsOnly: true to @vercel/nft
--ignore <pattern> Ignore pattern passed to @vercel/nft (repeatable)
--paths <alias=target> Resolution path mapping passed to @vercel/nft (repeatable)
--output <dir> Output directory (default: node_modules_optimized)
--dry-run Trace and report without writing files
--verbose Print entry files and NFT warningsExamples:
optimize-deps src/main.js src/workers
optimize-deps --exports-only --ignore "node_modules/pkg/test/**" src/main.js
optimize-deps --paths "utils/=src/utils/" src/main.js
optimize-deps --replace src/main.jsWith --replace, the existing node_modules directory is first moved to a temporary backup. The optimized directory is then renamed to node_modules, and the backup is deleted only after the rename succeeds.
In Docker/overlayfs builds, renaming a node_modules directory created in an earlier layer can fail with EXDEV: cross-device link not permitted. optimize-deps handles that by falling back to a copy-and-remove move that preserves file modes and symlinks.
