13

Is there a way to install NPM command line tools on NixOS?

[root@ip-xxx-xxx-0-27:~/teros/ntrs-cli]# sudo npm i -g typescript
npm WARN checkPermissions Missing write access to /nix/store/rhikjv5vlpa6vq4qkrszinwsaz1mda7p-nodejs-8.15.1/lib/node_modules
npm ERR! path /nix/store/rhikjv5vlpa6vq4qkrszinwsaz1mda7p-nodejs-8.15.1/lib/node_modules
npm ERR! code EROFS
npm ERR! errno -30
npm ERR! syscall access
npm ERR! rofs EROFS: read-only file system, access '/nix/store/rhikjv5vlpa6vq4qkrszinwsaz1mda7p-nodejs-8.15.1/lib/node_modules'
npm ERR! rofs Often virtualized file systems, or other file systems
npm ERR! rofs that don't support symlinks, give this error.

I assume because it's read-only, because I did run:

chown -R `whoami` nix/store/rhikjv5vlpa6vq4qkrszinwsaz1mda7p-nodejs-8.15.1

as an aside if someone knows how to install Node.js version 11 or 12 on nixos that'd be great.

Emmanuel Rosa
  • 9,697
  • 2
  • 14
  • 20
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

5 Answers5

14

Firstly, please undo the permissions change (chown) you made. You should NEVER change the permissions of files in the Nix store (/nix/store).

To install NPM packages on NixOS use the corresponding Nix package, instead of using npm -g .... NPM packages are under the nodePackages "namespace".

For example, to install typescript (tsc) edit /etc/nixos/configuration.nix:

...

environment.systemPackages = with pkgs; [
  ...

  nodePackages.typescript;
]

...

Then use nixos-rebuild switch to "install" the package.

You can install Node.js the same way. Use nix search nodejs to see the various versions you can install.

Emmanuel Rosa
  • 9,697
  • 2
  • 14
  • 20
10

Instead Edit ~/.npmrc so that it tells npm to install and find "global" packages in your home folder instead of the root location:

prefix=~/.npm-packages

now any time you run npm i -g <some-package> you will see that it will be installed inside of ~/.npm-packages.

Now in your shell rc file (f.e. .bashrc or .zshrc or similar), you'll need to update your PATH to include executables from the new location:

### Add NPM executables to your PATH so that they are available as commands:
export PATH="$HOME/.npm-packages/bin:$PATH"

Often it is more convenient to manage ephemeral dependencies outside of the system-level package manager.

If you use something like n or nvm to manage specific node versions, you can do a similar thing by managing them in your home folder.

trusktr
  • 44,284
  • 53
  • 191
  • 263
1
npm config set prefix '~/mutable_node_modules'

This thread should be helpful:

Adrian Miranda
  • 315
  • 3
  • 9
0

You can also install them with the usual npm -i..., just without the -g (global) paramater.

Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286
0

looks like things may have changed a bit. on fish shell with node v16.15.0 and nix (Nix) 2.8.1. I just had to add fish_add_path -g $HOME/.node_modules/bin to my config.fish then npm install -g <package>.

Creator54
  • 171
  • 9