This is my flakeless configuration for macOS using nix
{ config, ... }:
{
  home.sessionVariables.NPM_CONFIG_USERCONFIG = "${config.xdg.configHome}/npm/npmrc";
  xdg.configFile."npm/npmrc" = {
    text = ''
      cache=${config.xdg.cacheHome}/npm
      init-module=${config.xdg.configHome}/npm/init.js
    '';
  };
}