双盘笔记本中.npm cache与node_modules的最优存储位置及优化建议
Awesome question—let's dive into tuning your npm setup for that 256GB SSD + 1TB HDD laptop (with /home on HDD) and nvm usage. We'll cover cache, node_modules placement, and extra tweaks to get the best speed possible.
.npm Cache: Move It to the SSD
Looking at your npm config, your current cache is stored on the HDD at /home/username/.npm:
cache = "/home/username/.npm"
npm's cache gets a ton of read/write activity—downloading package tarballs, storing cached versions, etc.—so moving this to your SSD will make installs, updates, and package lookups way faster. Here's how to do it:
- First, pick a spot on your SSD for the cache. Let's assume your SSD is mounted at
/mnt/ssd(adjust this path to match your actual SSD mount point):mkdir -p /mnt/ssd/npm-cache - Update your npm config to use this new SSD-based cache:
npm config set cache /mnt/ssd/npm-cache --global - Verify the change worked by running
npm config get cache—it should return your new SSD path.
node_modules: Optimize Global and Project-Specific Locations
Global Packages (Managed by NVM)
Your current global package prefix is set to the HDD:
prefix = "/home/username/.nvm/versions/node/v10.15.3"
Since global packages are accessed frequently, moving your entire nvm installation to the SSD will speed up global installs and module loading. Here's how:
- Close all terminal windows to make sure no node processes are running.
- Copy your existing nvm folder to the SSD:
cp -r /home/username/.nvm /mnt/ssd/.nvm - Edit your shell config file (
.bashrc,.zshrc, or whatever you use) to update theNVM_DIRenvironment variable:export NVM_DIR="/mnt/ssd/.nvm" - Restart your terminal, then check
echo $NVM_DIRandnvm lsto confirm everything still works as expected.
Project-Specific node_modules
If your projects live on the HDD (since /home is there), installing node_modules directly to the HDD will be slow. You have two solid options:
Move high-priority projects to the SSD: If you're actively working on a project that needs frequent dependency updates or fast module access, just move the whole project folder to your SSD (e.g.,
/mnt/ssd/projects/my-cool-app). This is the simplest, most straightforward fix for performance-critical projects.Use symlinks to keep projects on HDD, node_modules on SSD: For projects you want to keep on the HDD, create a node_modules directory on the SSD and symlink it to your project:
# Create a directory for this project's node_modules on SSD mkdir -p /mnt/ssd/project-node-modules/my-cool-app # Navigate to your project on HDD cd /home/username/projects/my-cool-app # Remove any existing node_modules folder rm -rf node_modules # Create the symlink ln -s /mnt/ssd/project-node-modules/my-cool-app node_modulesNow when you run
npm install, all dependencies will be stored on the SSD, but your project files stay right where they are on the HDD.
Extra Tips for Even Better Performance
- Stick with
package-lock.json: You already havepackage-lock = trueenabled in your config—great! This file speeds up installs by skipping dependency resolution on every run, since npm can just use the exact versions from the lockfile. - Use
npm ciinstead ofnpm install: For clean, fast installs (especially in CI environments or when setting up a project for the first time),npm ciuses the lockfile directly and skips redundant steps. - Clean up old cache: Periodically run
npm cache clean --forceto remove stale packages and free up space on your SSD. - Try a faster package manager: Tools like
pnpmuse hardlinks to share dependencies across projects, saving space and drastically speeding up installs.yarnalso offers faster install times than npm in many cases. - Leverage shared stores: If you use pnpm, it automatically uses a shared store for dependencies. For npm, you can look into tools like
npm-cacheto share cached packages across projects.
内容的提问来源于stack exchange,提问作者KiriSakow

