How to reset npm's local install directory to its default?

I’m facing an issue with how npm installs packages locally. When I run npm install somepackage, it puts the modules in ~/src/node_modules/ instead of the expected ./node_modules. I suspect I might have altered some settings during a late night session, but now I can’t find out how to revert those changes.

I tried executing npm config set root ./node_modules, but running npm root still shows ~/src/node_modules. I even checked my .bash_profile for any modifications related to node, but nothing seems off.

Does anyone have any suggestions on how to restore npm to its default local install directory? Any help would be greatly appreciated!

Example scenario:

$ pwd
/home/user/myproject

$ npm install express
...
added 57 packages in 2s

$ ls node_modules
# Empty!

$ ls ~/src/node_modules
express, other-packages, ...

I’m really stuck on this, so any advice is welcome.

I’ve encountered a similar issue before, and it can be quite frustrating. Here’s what worked for me:

First, check for any .npmrc files in your home directory or project folder. These can override default settings. If you find any, review and remove any custom configurations related to install directories.

Next, try running npm config delete prefix to remove any custom prefix settings. This often resolves issues with non-standard install locations.

If that doesn’t work, you might want to clear your npm cache with npm cache clean --force. Sometimes cached data can cause weird behaviors.

As a last resort, consider uninstalling and reinstalling npm. I’ve had to do this a couple of times, and it usually fixes any lingering configuration issues.

Remember to always double-check your environment variables too. A misconfigured NODE_PATH can sometimes cause npm to behave unexpectedly.

Hope this helps you get back to your default setup!

Have you checked for any global npm configurations? Run npm config ls -l to see all settings, including defaults. Look for anything related to prefix or root. If you spot something off, try npm config delete <setting> to remove it.

Also, check your home directory for a .npmrc file. If it exists and has odd settings, either edit or remove it. Sometimes, a rogue global config can cause this behavior.

As a last resort, you might want to consider reinstalling npm. You can do this with npm install -g npm@latest. This often resolves weird configuration issues.

Remember to clear your npm cache with npm cache clean --force after making changes. Then try installing a package in a new project directory to see if the issue is resolved.

hey man, sounds like u messed with npm’s settings.
try checkin for any .npmrc files in ur project or home folder.
if u find any, delete em or remove any weird settings.
then run npm config rm prefix to reset stuff.
after that, delete node_modules and run npm install again. that should fix it!