14

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.

5 Answers 5

13

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.

1
  • 1
    I tried to install browser-sync, but nix can't find it ...
    – rambi
    Commented May 21, 2021 at 20:53
11

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.

2
npm config set prefix '~/mutable_node_modules'

This thread should be helpful:

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>.

-1

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

2
  • This is about NixOs. You can't just run the npm command. Commented Jul 12 at 23:38
  • Yes you can? Omitting the -g will mean it won't try to place the output into non writable paths but instead the local path. Commented Jul 14 at 9:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.