When to use sudo in macOS?
sudo is not discouraged on MacOS it is much to be preferred to the alternative which is to su to another user especially root. It is discouraged to set up root so you can login or su to it.

Normally you can only write to files in directories you have write acces to.

If you need to write to a file that is not ina directory owned by you your will not usually have permission. e.g. /usr/local/bin can only be written to by root.

If you need to copy a file to a protected directory - as the npm install does you have to ask for more privileges. Adding sudo before a command means that the command is run as if root was running it anmd so the command can write to /usr/local/bin

sudo can be used to run as any user by using the -u parameter.