Why am I getting an older version of npm when I have nodejs?

I’m getting real weirded out, I have this flake

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
  };

  outputs = { self, nixpkgs, ... }: 
  let
    system = "x86_64-linux";
    pkgs = import nixpkgs { inherit system; };
  in
  {
    devShell.${system} = pkgs.mkShell {
      buildInputs = [ pkgs.nodePackages.npm ];
    };
  };
}

Then if I run

> nix develop
> npm -v
8.1.3

So everything’s fine. But then if I add nodejs:

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
  };

  outputs = { self, nixpkgs, ... }: 
  let
    system = "x86_64-linux";
    pkgs = import nixpkgs { inherit system; };
  in
  {
    devShell.${system} = pkgs.mkShell {
      buildInputs = [ pkgs.nodejs pkgs.nodePackages.npm ];
    };
  };
}

And then I run

>nix develop
>npm -v
6.14.15

Which is weird enough, but then if I change the order of the packages to

buildInputs = [ pkgs.nodePackages.npm pkgs.nodejs ];

Then

>nix develop
>npm -v
8.1.3

What is happening here?

1 Like

pkgs.nodejs is currently aliased to pkgs.nodejs-14_x, which distributes npm==6.14.15.
nodePackages.npm follows what is available in npm, so it should be latest.

The reason why the ordering matters, is that when nix is creating a shell, it’s also constructing the PATH to include your dependencies. So the ordering determines what will be found by the shell first.

If you want a newer version, you could also select a newer version of node. There’s nodejs-17_x which distributes npm==8.1.0

4 Likes

Nice! I had figured out the first part of the question shortly after but this, I wouldn’t have known in a thousand years. Thanks!

well, i find diffoscope quite good for things like this.

it actually diffs shell environment outputs really nicely, it could be nicer, but maybe i can work on it.

so, if you the first flake

nix develop
env > 1.txt

and the second flake

nix develop
env >  2.txt
nix-shell -p diffoscope
diffoscope --html enviromentdiff.html 1.txt 2.txt

run diff’s on nix environments might be a nice tool to write.

3 Likes