We’re shipping 3 executables in the node package: node, npm and yarn. I’d like to get npm lifted to a top-level command via an alias please. We need to do a bit more testing on yarn with the Yarn team before we proceed so I’ll add that as a separate request.
Why does npm from node snap require root to install packages? Won’t that make a lot of problems in using frameworks like angular when building and running the app?
aresminos@desktop:~$ npm install -g typescript
npm WARN checkPermissions Missing write access to /usr/local/lib
npm ERR! path /usr/local/lib
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall access
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib'
npm ERR! { Error: EACCES: permission denied, access '/usr/local/lib'
npm ERR! stack: 'Error: EACCES: permission denied, access \'/usr/local/lib\'',
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'access',
npm ERR! path: '/usr/local/lib' }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/aresminos/.npm/_logs/2018-05-10T09_30_17_145Z-debug.log
Here is a solution but shouldn’t something in the snap be changed? This solution is not the default, and afaik npm never asked for root to install global packages. Anyway, follow these links:
This is a standard problem with Node, not specific to the snap install by the way. It’s not a great idea to use sudo but there are some typical ways of getting around this:
Avoid -g if you can, seriously. Many install instructions will have -g but they don’t really need it. Do you really want to clutter up your global space with things you’re not going to re-use? If you need a command-line utility then maybe npx (https://github.com/zkat/npx) or reaching in to node_modules/.bin/ is enough? Also keep in mind that anything in "scripts" in your package.json has node_modules/.bin/ in its PATH so they don’t need to be globals if you are scripting something up for a node package.
chown $USER -R /usr/local/ - this has become a common pattern on developer (single-user) machines. If you own the machine and you’re the only one on it, then it’s not a bad thing to just take ownership of /usr/local. I do this on my machines and it gives me -g access without sudo, amongst other benefits.
Set your own npm prefix. For the snap, it’s set to /usr/local, but it could also be /home/me/node_global/. Anything you install with -gwill go into$prefix/lib/node_modules/ with executables in $prefix/bin/. Use npm config set prefix /home/me/whatever to change it, just put that path + /bin/ into your PATH and you have what you need.
There’s not much for the snap to do here, it’s not going to set up a per-user $prefix since the standard behaviour is for -g to be for global executable scripts. It would also be wrong for it to escalate privileges without asking you, so an opt-in sudo is appropriate here if it’s going to be installing things outside of a space that you as a user have access to.