Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install NPM end-user packages on NixOS?

Tags:

chown

nix

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.

like image 710
Alexander Mills Avatar asked Jun 28 '19 21:06

Alexander Mills


People also ask

What does meteor npm install do?

The meteor-node-stubs npm package provides browser-friendly implementations of Node's built-in modules, like path , buffer , util , etc. Meteor's module system avoids actually bundling any stub modules (and their dependencies) if they are not used, so there is no cost to keeping meteor-node-stubs in the dependencies.


3 Answers

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.

like image 71
Emmanuel Rosa Avatar answered Oct 07 '22 12:10

Emmanuel Rosa


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.

like image 36
trusktr Avatar answered Oct 07 '22 14:10

trusktr


npm config set prefix '~/mutable_node_modules'

This thread should be helpful:

  • https://github.com/NixOS/nixpkgs/issues/3393#issuecomment-50330167
  • https://github.com/NixOS/nixpkgs/issues/3393#issuecomment-187510024
like image 34
Adrian Miranda Avatar answered Oct 07 '22 12:10

Adrian Miranda