Published: 10.12.2020 | Edited: 22.12.2020 | Tags: node

Don't use global npm config for dotfiles with nvm

It is possible to set the global npm config via the global switch

# short version
npm config set init-version "0.0.1" -g

# long version
npm config set init-version "0.0.1" --global

The location of the global npm config in nvm is tied to node version, rendering it unsuitable for dotfiles

$ npm config get prefix
/home/peterbabic/.nvm/versions/node/v15.4.0

The actual file location is thus {prefix}/etc/npmrc1

/home/peterbabic/.nvm/versions/node/v15.4.0/etc/npmrc

When installing new node version with nvm, the config file has to be copied over

nvm install 15.4
cp ~/.nvm/versions/node/v14.9.0/etc/npmrc ~/.nvm/versions/node/v15.4.0/etc/npmrc

Not using global setup

Here's how I store npm config among the dotfiles, using so called userconfig instead of a global config 2

  • Install nvm
sudo pacman -S nvm
  • Install node version of your liking, i.e. stable release3
nvm install stable
  • Configure the init values
npm config set init-version "0.0.1"
npm config set init-author-email "your@email.here"
npm config set init-author-name "Peter Babič"
npm config set init-license "MIT"
npm config set init-author-url "https://peterbabic.dev"

Alternatively, paste the values into ~/.npmrc manually

init-author-name=Peter Babič
init-version=0.0.1
init-author-email=your@email.here
init-license=MIT
init-author-url=https://peterbabic.dev
  • Store the file among your dotfiles, i.e. via yadm4
yadm add ~/.npmrc && yadm commit
  • Initialize the the project promting the values
npm init -y

Produces the pre-configured package.json file straight away, saving time

{
  "name": "project",
  "version": "0.0.1",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "Peter Babič <peter@babic.dev> (https://peterbabic.dev/)",
  "license": "MIT"
}

Used versions for the completenes

$ nvm --version
0.35.2

$ npm --version
7.0.15

$ node --version
v15.4.0

$ yay -Qi yadm | grep Version
Version         : 2.5.0-1

Further reading