I’ve switched getting Node tools via either my per-project, flake.nix or downsteam in the inputs of a derivation or if necessary in my global Nix config—because if I don’t, I’ll forget to update them and I’m wary of anything suggesting to install it globally.
Aside: it’s ridiculous that npm denied people wanting them to use XDG base dirs on Linux because one of the maintainers personally likes it more at the root of the home directory.
You can just use nvm (or one of the other managers) and you won’t run into needing be root to globally install packages. These days npx eliminates a lot of the cases where you might have previously installed a package globally.
Yup - the author includes the suggestion of nvm or Volta at the end. I will note, however, that installing something globally is immediately invalidated as soon as you change your nvm version.
The author also makes npx sound like such a burden, but it’s actually the best method moving forward as it’s either global OR local to a project, depending on if the project has installed it. I don’t buy the “extra words to run a command” argument - especially since they try to sell needing to cd into a specific directory as a valid option.
Snippet straight from my .zshrc for those who like xdgisms
# BEGIN js #
if hash npm 2>/dev/null; then
if [[ ! -f $XDG_CONFIG_HOME/npm/config ]]; then
mkdir -p $XDG_CONFIG_HOME/npm $XDG_DATA_HOME/npm
print -- "prefix=$XDG_DATA_HOME/npm"
print -- "cache=$XDG_CACHE_HOME/npm"
} > $NPM_CONFIG_USERCONFIG
if hash yarn 2>/dev/null; then
alias yarn='yarn --use-yarnrc $XDG_CONFIG_HOME/yarn/config '
# force some other XDG stuff
if [[ ! -f $XDG_CONFIG_HOME/yarn/config ]]; then
mkdir -p $XDG_CONFIG_HOME/yarn $XDG_DATA_HOME/yarn
print -- "prefix \"$HOME/.local\""
print -- "yarn-offline-mirror \"$XDG_DATA_HOME/yarn/local-mirror\""
} > $XDG_CONFIG_HOME/yarn/config
# END js #
on the package.json limitations of npm:
yarn (v1) actually does create a package.json for things installed globally at “$prefix/global/package.json”. Shame devs apparently hate global installs? I think neo-yarn is all about the new zipfile modules and stuff now?
But it’s worth looking into yarn v1 if you want this behavior or just want to globally install stuff.