Can you use `npm link` with a nix shell?

I have 2 separate node applications, one of which is dependent on the other. In this case I would expect npm link to allow one to reference the other, but with NixOS I get the following

The node_modules directory does exist.

I am running the two separate apps within nix-shell -p nodejs_21 shell, which I thought would the npm linking to work. From the looks of it everything within the /nix/store/ is read-only (which makes sense). So is using npm link possible?

Yes you can, this is related to this bug. The solution is to tell npm to use a writable place for global changes, either manually by adding in ~/.npmrc a line prefix=~/.npm, or, simpler, by enabling this automatically via:

# Avoid bugs with npm like https://github.com/NixOS/nixpkgs/issues/16441
programs.npm.enable = true;

Then, either reboot to load the environment variables (the module configures NPM_CONFIG_GLOBALCONFIG to point to /etc/npmrc), or load them in the current shell via source /etc/set-environment. Then, npm link works as before!

1 Like