Installing node packages globally in Google Cloud Shell

Google Cloud Shell comes pre-provisioned with a lot of useful packages, including node. And your Shell VM is preserved across sessions. Installing a node package using

npm install -g <package>

does not work because of insufficient privileges.

And trying to install by:

sudo npm install -g 

does not work, because there is no path to npm for the root account.

To work around it, you must follow the procedure in this article:

echo $NODE_PATH    #  use this value in a minute
As root edit its file ~root/.bashrc and add this line
export NODE_PATH=value_from_above_echo
now identify PATH to reach node ... issue following as yourself
which node
similarly as root edit ~root/.bashrc and add this line
export PATH=value_of_dir_from_above_which:${PATH}
Once you have made these two changes to the root env vars then root can issue node and npm commands ... to engage changes either create fresh root shell session or as root source its ~/.bashrc
source ~/.bashrc

Comments

Popular Posts