in <b>linux</b>, sudo invokes a new shell, and the environmental variables will be reset, see the lines in /etc/sudoers:
Defaults	env_reset
Defaults	secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
the PATH in the shell which calls sudo won't be passed to the new shell.
it will result in an error: npm command can not be found.
Thus the pass a full path of npm will fix this issue.