How to install NPM end-user packages on NixOS? - nix

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.

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.

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.

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

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

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

Related

How to install expo CLI on macbook pro big sur?

I an having issue regarding the installation of expo on the new mac.
When I run this command:
npm install --global expo-cli
I have the error:
npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /usr/local/lib/node_modules
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR! [Error: EACCES: permission denied, access '/usr/local/lib/node_modules'] {
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'access',
npm ERR! path: '/usr/local/lib/node_modules'
npm ERR! }
npm ERR!
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR!
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/theowatine/.npm/_logs/2020-11-21T09_26_14_033Z-debug.log
Does anyone know what could be the solution? Thank you for your help!
This is a common issue when using a globally-installed npm. The npm docs contain a page about it.
They recommend reinstalling node/npm with a version manager (like nvm). This is often a very good option if you are developing something locally. If you do that, this problem will go away. You can stop reading this answer if you want.
They also provide a way to configure npm to put the globally installed items in a directory of your choosing. That is a good option too, if a version manager won't work for you for whatever reason.
Another option they mention is using npx instead of globally installing the tool. I use this one a lot and like it quite a bit, but it does mean that you frequently have to remember to prefix commands you see with npx.
There are two somewhat common options that they do not list. One is to run your install with sudo. This means that any lifecycle scripts will get run as root on your machine. This is generally a bad idea (which is why they don't mention it in the docs), but is very common.
Another option is to chmod your /usr/local/lib/node_modules directory such that you can write to it. This is also a not-great idea in my opinion (which is probably why it too is not mentioned in the docs), but people do it.
In the case of expo-cli and in most developing-something-locally cases, installing with a package manager like nvm is probably the way to go.
I am also using macOS Big Sur on mbp and i experienced same issue. After reading Trott's answer i downloaded node from https://nodejs.org/en/download/ and installed it. Now expo and react native working fine.
Homebrew version of node is failed to install expo at all time but direct download from nodejs.org worked fine.
try this:
sudo npm install -g expo-cli
I am using Big sur(11.4) and I have downloaded latest version of node
https://nodejs.org/en/download/
Then run the following command for install expo and its working fine.
npm install --global expo-cli

I wanna reinstall expo-cli, but there is err. And can't uninstall expo-cli

I tried to build the app through expo but failed, and I tried to reinstall expo-cli.
So, I entered the code below to remove the expo-cli.
npm -g uninstall expo-cli --save
and
(base) Bongui-MacBookPro:expo-firebase-master bong$ npm uninstall expo-cli -g
up to date in 0.042s
However, if i type expo, it will run.
So if you ignore it again and try to reinstall expo-cli,
The following errors occur:
npm ERR! code EEXIST
npm ERR! syscall symlink
npm ERR! path ../lib/node_modules/expo-cli/bin/expo.js
npm ERR! dest /Users/bong/npm-global/bin/expo-cli
npm ERR! errno -17
npm ERR! EEXIST: file already exists, symlink '../lib/node_modules/expo-cli/bin/expo.js' -> '/Users/bong/npm-global/bin/expo-cli'
npm ERR! File exists: /Users/bong/npm-global/bin/expo-cli
npm ERR! Remove the existing file and try again, or run npm
npm ERR! with --force to overwrite files recklessly.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/bong/.npm/_logs/2020-01-28T09_18_38_604Z-debug.log
Thank you, teachers.
Please help me solve the problem.
Thank you.
I think you might want to overwrite your current folder while you building.
Try and use the --force extension in your command and that will resolve the problem. I presume something like : npm install -g --force expo-cli
I hope that will help.
I am using linux , Ubuntu i tried
sudo npm install -g --force expo-cli
it worked for me.

Getting npm Err! tar.unpack while setting up cordova environment

I followed this tutorial to install yeoman, bower, grunt to setup angularjs corodva project. http://www.sitepoint.com/kickstart-your-angularjs-development-with-yeoman-grunt-and-bower/
as it says i installed everything but still it gives me error! while creating angular project.
I did,
My PC is running windows 8.1 64 bit
Install node.js using windows installer (64 bit)
Install git (selected windows commandline instead git bash while installing)
npm install -g yo grunt-cli bower
I cross checked everything whether it is installed
C:\Windows\system32>yo --version && grunt --version && bower --version
yo - 1.4.7
grunt-cli v0.1.13
bower - 1.4.1
4. npm install -g generator-angular
created a dir in c and installed yo angular
C:\test\yo angular
It asked for sass (i given NO), bootstrap (YES), other angularjs library (NO) then it started installing
then after sometime, it asks that my package.json should overwrite i gave yes
after the few files generated then shows ERR!
npm ERR! tar.unpack untar error /root/.npm/wrappy/1.0.1/package.tgz
npm ERR! tar.unpack untar error /root/.npm/wrappy/1.0.1/package.tgz
npm ERR! tar.unpack untar error /root/.npm/brace-expansion/1.1.0/package.tgz
npm ERR! tar.unpack untar error /root/.npm/delayed-stream/0.0.5/package.tgz
npm WARN optional dep failed, continuing form-data#0.1.4
npm ERR! tar.unpack untar error /root/.npm/is-property/1.0.2/package.tgz
npm WARN optional dep failed, continuing request#2.54.0
As Prasanth says, The best way is to do the following:
npm cache clean
double checked whether everything is installed
also try close then re-open cmd as administrator
When angular asks for overwrite package.json, Say n to all
Still faces issue, then try this
make sure Grunt is installed
npm install (it will install missing packages)

iOS Yosemite sudo npm install -g DYLD_ environment

I have a problem after I upgraded my machine to Yosemite, whenever I run sudo npm install -g ... I got an error says
/usr/bin/dyld: DYLD_ environment variables being ignored because main executable (/usr/bin/sudo) is setuid or setgid
For example was trying to install gulp using
sudo npm install -g gulp
and got the same error ! not able to find the reason behind this issue
Generally this means that an existing, globally installed package is in bad shape. Possibly permissions related, but hard to say for sure without more information about the environment.
The solution lies in fixing the broken package. One way to do so is delete and re-install it. Try sudo npm uninstall -g <pkg> where <pkg> is anything you suspect may be broken.
If you are not sure where to start, do:
npm list -g --depth=0
That will show you all of your globally installed packages. Just remove and re-install them one by one.
If any of those throw errors during the removal process, you will need to do it yourself, manually.
First, figure out where global packages are installed. Generally, this will be /usr/local on OS X, for example. To find out for sure:
npm config get prefix
Then explore that directory, especially /usr/local/bin (for example) and if you are sure a file is related to a global npm package, it's usually safe to remove it.
`sudo rm -rf <file>`
... where <file> is the actual filename you wish to delete.
You may have gotten into this mess due to overuse of sudo.
See my answer to SNAFU with npm update -g on why sudo is bad and instructions on how to fix it.

How to remove a Yeoman generator

I accidentally installed a generator that I don't want.
I can't find any method to remove it.
What should I do to accomplish this?
Generators are just normal npm modules, so you can remove it with
npm uninstall -g generator-[nameOfGenerator]
npm uninstall -g [generator-name] might not fix the UNMET DEPENDENCY.
If you won't mind to reinstall the affected modules:
cd to your npm directory (e.g. /usr/local/bin/node_modules)
rm -rf [generator-name]
npm cache clean
npm install -g [generator-name]
Search for generators with
npm list -g --depth=0 | grep 'generator'
Remove generator with
npm uninstall -g [generator-name]
NOTE: Don't include '#[version] ' that follows the generator name
I wanted to add something for posterity:
If you get an number of errors of class 'peerDependencies' for yeoman's generators, updating yo alone will not cut it - you need to remove all generators that throw the error and then reinstall whatever module lead to the trouble.
Unfortunately, as far as I ahve chekced, regex is not supported by npm.

Resources