[Answer]-"your cache folder contains root-owned files, due to a bug in npm err! previous versions of npm which has since been addressed."


Error "Your cache folder contains root-owned files, due to a bug in previous versions of npm"

Error Solution:

To resolve the error regarding root-owned files in your npm cache folder, you can follow these steps:

  1. Open your terminal or command prompt.
  2. Navigate to your project directory where you encountered the error.
  3. Run the following command to change the ownership of the cache folder to your current user:
    sudo chown -R $(whoami) ~/.npm
    This command will recursively change the ownership of all files and folders inside the npm cache folder to your user account, resolving any permission issues.
  4. Once the ownership has been changed, try running your npm commands again to see if the error persists.

If you continue to encounter issues, consider updating npm to the latest version to ensure you're using the most stable release with the bug fixes.

If you encounter this issue and lack root access to rectify the permissions for the current NPM cache folder, you can circumvent the error by defining a different cache directory. First, create a new directory for the cache. Then, set the npm_config_cache environment variable: In Linux:
export npm_config_cache=/path/to/cache
In Windows:
set npm_config_cache=/path/to/cache

This command worked for me:

sudo chown -R 1000:1000 "~/.npm"

I resolved the issue using the following method:

First, execute:

sudo npm cache clean --force

Then, remove the files owned by root. To view the file owners and groups:

cd /Users/username/
ls -la

Check the owner of .npm and change it to a non-root user:

sudo chown -R username:group "/Users/username/.npm"

Or for global .npm-global:

sudo chown -R username:group "/Users/username/.npm-global"

Simply updating the npm version and deleting the entire project did the trick.