Alexander Mills
Alexander Mills

Reputation: 100010

How to install NPM end-user packages on NixOS?

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.

Upvotes: 16

Views: 9792

Answers (5)

Creator54
Creator54

Reputation: 171

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

Upvotes: 0

Adrian Miranda
Adrian Miranda

Reputation: 334

npm config set prefix '~/mutable_node_modules'

This thread should be helpful:

Upvotes: 2

trusktr
trusktr

Reputation: 45464

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.

Upvotes: 12

Chris Stryczynski
Chris Stryczynski

Reputation: 33891

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

Upvotes: -2

Emmanuel Rosa
Emmanuel Rosa

Reputation: 9885

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.

Upvotes: 13

Related Questions