Cause 1 of 4
Node was installed system-wide, so its global directory belongs to root.
Install Node through a version manager that keeps everything under your home directory — nvm, fnm, Volta or asdf. After nvm install --lts, global installs need no elevated permissions at all. This is the fix npm's own documentation recommends, and it also lets you switch Node versions per project.
Cause 2 of 4
You want to keep the system Node but stop the permission errors.
Point npm's global prefix at a directory you own: mkdir ~/.npm-global then npm config set prefix ~/.npm-global, and add ~/.npm-global/bin to PATH in your shell profile. New global installs land there and need no sudo.
Cause 3 of 4
The package does not need to be global at all.
Run it once with npx <tool>, or add it to the project's devDependencies and call it from an npm script. Project-local tools also pin the version per repository, which a global install cannot.
Cause 4 of 4
An earlier sudo install left root-owned files in your cache or project.
Give the cache back to your user: sudo chown -R $(id -u):$(id -g) ~/.npm. Check the exact path printed in the error before running it, and never widen permissions with chmod 777 — that lets any account on the machine modify code you are about to execute.