npm install -g without sudo
Missing write access to /usr/local/lib/node_modules is not write permission, npm official solution is to create a new folder with permissions, install npm package in this new folder. This method does not apply to Microsoft Windows system.
Create a folder in the user’s root directory (the name does not have to be .npm-global, you can do it yourself):
Set the installation path of the npm global package:
npm config set prefix '~/.npm-global'
Check if there is a .profile file in the user’s root directory. If it is not created, open it with a text editor and add the following line to save:
Go back to the terminal and run the following command to make the configuration take effect:
Then install the npm package globally.