snap store node js not let me install angular cli

I am on Ubuntu 20.04 and I installed the node from snap store. Now when I am trying to install the angular cli, it doesn't let me do that

2020/03/28 11:51:57.975509 cmd_run.go:884: WARNING: cannot create user data directory: cannot create "/nonexistent/snap/node/2622": mkdir /nonexistent: permission denied
cannot create user data directory: /nonexistent/snap/node/2622: Permission denied
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! @angular/[email protected] postinstall: `node ./bin/postinstall/script.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the @angular/[email protected] postinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-03-28T06_22_03_201Z-debug.log

Anyone have any idea for this, this is look like permission issue. How I can install the ng cli into my 20.04 system.


Solution 1:

The problem here is, that snap does not allow installed snap packages to write to /usr/local. Unfortunately this is the default location from global installed npm packages.

You can solve this issue in 3 simple steps by changing the default location from global installed npm packages to one directory, where the snap (in this case node) is allowed to write.

At first we createthe new directory for our future global installed npm packages:

mkdir ~/.local/npm

Now we need to add the new created folder to our PATH. For this open or create a ~/.bashrc file and add this line:

export PATH=~/.local/npm/bin:$PATH

Do you noticed the new /bin folder and wonder why this one is here? Npm will create it and it will contain the executeables/syslinks for global installed packages. Without it, you would not be able to run ng in your terminal.

The last step is to override the default npm global location (npm calls this "prefix"):

npm config set prefix '~/.local/npm'

Now you can install npm packages global. npm install -g @angular/cli should now work.

To test if everything is working correctly, run ng version after you installed it globally with npm.

I used the following source for my answer: https://github.com/mixonic/docs.npmjs.com/blob/master/content/getting-started/fixing-npm-permissions.md