nvm
nvm copied to clipboard
You have $NVM_DIR set to "/root/.nvm", but that directory does not exist. Check your profile files and environment.
I'm trying to upgrade node. So I installed nvm. Still running on the old version of node. So I uninstalled it. Now running on the nvm version. Except some modules were compiled for old version. Delete node_modules. NPM install. Permission error installing with node-gyp. Google. Someone said you can't use nvm as root. Uninstall NVM. Make another user. Install NVM. Error:
You have $NVM_DIR set to "/root/.nvm", but that directory does not exist. Check your profile files and environment.
Operating system and version:
ubuntu 16
nvm debug output:
nvm: command not found
nvm ls output:
nvm: command not found
How did you install nvm?
i didnt
What steps did you perform?
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
What happened?
You have $NVM_DIR set to "/root/.nvm", but that directory does not exist. Check your profile files and environment.
What did you expect to happen?
install nvm
Is there anything in any of your profile files that modifies the PATH?
probably maybe
If you are having installation issues, or getting "N/A", what does curl -I --compressed -v https://nodejs.org/dist/ print out?
* Trying 104.20.23.46...
* Connected to nodejs.org (104.20.23.46) port 443 (#0)
* found 129 certificates in /etc/ssl/certs/ca-certificates.crt
* found 516 certificates in /etc/ssl/certs
* ALPN, offering http/1.1
* SSL connection using TLS1.2 / ECDHE_RSA_AES_128_GCM_SHA256
* server certificate verification OK
* server certificate status verification SKIPPED
* common name: *.nodejs.org (matched)
* server certificate expiration date OK
* server certificate activation date OK
* certificate public key: RSA
* certificate version: #3
* subject: OU=Domain Control Validated,OU=PositiveSSL Wildcard,CN=*.nodejs.org
* start date: Mon, 21 Oct 2019 00:00:00 GMT
* expire date: Tue, 18 Jan 2022 23:59:59 GMT
* issuer: C=GB,ST=Greater Manchester,L=Salford,O=Sectigo Limited,CN=Sectigo RSA Domain Validation Secure Server CA
* compression: NULL
* ALPN, server accepted to use http/1.1
> HEAD /dist/ HTTP/1.1
> Host: nodejs.org
> User-Agent: curl/7.47.0
> Accept: */*
> Accept-Encoding: deflate, gzip
>
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Date: Mon, 08 Feb 2021 20:10:01 GMT
Date: Mon, 08 Feb 2021 20:10:01 GMT
< Content-Type: text/html
Content-Type: text/html
< Connection: keep-alive
Connection: keep-alive
< Set-Cookie: __cfduid=d200ab00463901fee642fdd5f6f2dbdd31612815001; expires=Wed, 10-Mar-21 20:10:01 GMT; path=/; domain=.nodejs.org; HttpOnly; SameSite=Lax
Set-Cookie: __cfduid=d200ab00463901fee642fdd5f6f2dbdd31612815001; expires=Wed, 10-Mar-21 20:10:01 GMT; path=/; domain=.nodejs.org; HttpOnly; SameSite=Lax
< Cache-Control: max-age=14400
Cache-Control: max-age=14400
< CF-Cache-Status: HIT
CF-Cache-Status: HIT
< Age: 4768
Age: 4768
< cf-request-id: 0824dd0116000037faa20c4000000001
cf-request-id: 0824dd0116000037faa20c4000000001
< Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
< Vary: Accept-Encoding
Vary: Accept-Encoding
< Server: cloudflare
Server: cloudflare
< CF-RAY: 61e7fde1be7837fa-IAD
CF-RAY: 61e7fde1be7837fa-IAD
< Content-Encoding: gzip
Content-Encoding: gzip
<
* Connection #0 to host nodejs.org left intact
ok, so first, nvm isn't meant to be used as root, it's per-user.
Second, npm rebuild is how you recompile your node_modules, you don't have to remove and reinstall them.
Third, when you made another user, did you log out and back in as it? or did you su to it, preserving the env vars from the root user's session?
I did su. I can't figure out how to switch another way. My IDE / SSH tool is configured to to log in as root and doesn't work if I change it to the other user. I tried to log out with a command but that just ended my session and when I went back in it logged me in as root again.
I'm not sure what tool you're using, but generally you provide the username you're logging in with as part of the connection.
Either way, you can unset $NVM_DIR after su and it should be fine.
now I get this:
lospec@lospec-beta:/$ sudo curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 13527 100 13527 0 0 100k 0 --:--:-- --:--:-- --:--:-- 100k
=> Downloading nvm from git to '/home/lospec/.nvm'
=> mkdir: cannot create directory ‘/home/lospec/.nvm’: Permission denied
ls: cannot access '/home/lospec/.nvm': No such file or directory
fatal: could not create work tree dir '/home/lospec/.nvm': Permission denied
Failed to clone nvm repo. Please report this!
you used sudo curl, don't do that. there's no sudo used in the readme.
ok well same result
lospec@lospec-beta:/$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 13527 100 13527 0 0 59142 0 --:--:-- --:--:-- --:--:-- 62625
=> Downloading nvm from git to '/home/lospec/.nvm'
=> mkdir: cannot create directory ‘/home/lospec/.nvm’: Permission denied
ls: cannot access '/home/lospec/.nvm': No such file or directory
fatal: could not create work tree dir '/home/lospec/.nvm': Permission denied
Failed to clone nvm repo. Please report this!
ok - if you're logged in as user, why do you not have full permissions and ownership over $HOME?
Because I have no idea how to use linux
edit: here is the permissions of the home dir:
root@lospec-beta:/home/lospec/htdocs# ls -l $HOME
total 69492
-rw-r--r-- 1 root root 357647 Apr 23 2018 export.json
-rw-r--r-- 1 root root 13 Jun 21 2017 gitput.sh
-rw-r--r-- 1 root root 47 Jun 25 2017 gitupdate.sh
-rw-r--r-- 1 root root 70664728 Apr 29 2019 mongodb-compass-community_1.17.0_amd64.deb
drwxr-xr-x 271 root root 12288 Jan 21 2019 node_modules
-rw-r--r-- 1 root root 108005 Jan 21 2019 package-lock.json
root@lospec-beta:/home/lospec/htdocs# su lospec
lospec@lospec-beta:~/htdocs$ ls -l $HOME
total 12
drwxr-xr-x 20 lospec lospec 12288 Feb 8 21:47 htdocs
edit: permissions are definitely bad.
lospec@lospec-beta:~/htdocs$ npm install npm ERR! code EACCES npm ERR! syscall mkdir npm ERR! path /home/lospec/.npm npm ERR! errno EACCES npm ERR! npm ERR! Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which has since been addressed. npm ERR! npm ERR! To permanently fix this problem, please run: npm ERR! sudo chown -R 1001:1002 "/home/lospec/.npm"
User management can be a nightmare when on Linux when you don't deal with it every day, suddenly things work or don't work and you have no idea what's going on until things start crashing because of permissions errors and the inability to access directories etc.
So let's see where we're at here: my assumption is you've ssh'd into a remote Linux server (Ubuntu? If so which version) which is hosting a site dependant on node. If this is not a development server and is fact a beta hosting server it may be that NVM is not ideal for your use case as it's unlikely you want to manage multiple Linux users and multiple node versions on this otherwise straight forward hosting server. (That isn't to say NVM wouldn't be useful, but it might be overkill for your current stage of development).
To figure out next steps: Are my assumptions so far correct? And what is the current install state of node on your system (for example what happens when your run 'which node' or 'node --version' or even 'npm -v')?
I've now got node 15.8 installed (which is what i want), but i think it's installed outside of nvm. yes it's ubuntu 16 with ssh.
It does seem to have a permission conflict as this error says, but my attempts to do what the error said to failed:
lospec@lospec-beta:~/htdocs$ npm install
npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /home/lospec/.npm
npm ERR! errno EACCES
npm ERR!
npm ERR! Your cache folder contains root-owned files, due to a bug in
npm ERR! previous versions of npm which has since been addressed.
npm ERR!
npm ERR! To permanently fix this problem, please run:
npm ERR! sudo chown -R 1001:1002 "/home/lospec/.npm"
lospec@lospec-beta:~/htdocs$ unset home
lospec@lospec-beta:~/htdocs$ ls -l $HOME
total 12
drwxr-xr-x 20 lospec lospec 12288 Feb 8 21:47 htdocs
lospec@lospec-beta:~/htdocs$ sudo chown -R 1001:1002 "/home/lospec/.npm"
[sudo] password for lospec:
chown: cannot access '/home/lospec/.npm': No such file or directory
lospec@lospec-beta:~/htdocs$ cd ..
lospec@lospec-beta:~$ cd ..
lospec@lospec-beta:/home$ cd ..
lospec@lospec-beta:/$ cd ..
lospec@lospec-beta:/$ cd ..
lospec@lospec-beta:/$ sudo chown -R 1001:1002 "/home/lospec/.npm"
chown: cannot access '/home/lospec/.npm': No such file or directory
lospec@lospec-beta:/$ sudo chown -R 1001:1002 "$HOME/lospec/.npm"
chown: cannot access '/home/lospec/lospec/.npm': No such file or directory
lospec@lospec-beta:/$ cd home
lospec@lospec-beta:/home$ cd lospec
lospec@lospec-beta:~$ sudo chown -R 1001:1002 ".npm"
chown: cannot access '.npm': No such file or directory
lospec@lospec-beta:~$ node --version
v15.8.0
Could you also do a :
apt list --installed | grep nodejs
and
apt list --installed | grep npm
just to make sure if we could fix this by purging and reinstalling from apt
Also, have you done a npm init?
You might be past this already, but from your user home folder (cd ~ can be used as a short cut to get there) what is the result of ls -la?
You might be past this already, but from your user home folder ('cd ~' can be used as a short cut to get there) what is the result of 'ls -la'?
lospec@lospec-beta:~$ cd ~
lospec@lospec-beta:~$ ls -la
total 20
drwxr-xr-x 3 root root 4096 Mar 16 2020 .
drwxr-xr-x 6 root root 4096 Feb 8 18:34 ..
drwxr-xr-x 20 lospec lospec 12288 Feb 8 21:47 htdocs
Ok, I think I figured it out.
Use sudo npm install --unsafe-perm=true --allow-root instead
That should do the trick.
Ok, I think I figured it out.
Use
sudo npm install --unsafe-perm=true --allow-rootinstead
@Lucinotion Do you think they would have any security concerns with granting node that level of access though? Granted it might be the only way for this set up to work smoothly, but essentially scripts running in node would now have full system level privilege wouldn't they? Probably a non-issue in this use case but just something to think of 🤷♂️
@skeddles In any case if the quoted solution works, awesome! Otherwise it looks like the npm cache is pointing at a location in that user home directory that doesn't exist for that user (the ".npm" folder). npm cache clean --force might help reset that cache and then a clean npm install inside the directory where your package.json is located may work/show more useful errors.
Since node is installed at the right version now you should be right at the finish line here, just some ghosts of the journey to get here still causing bugs! Ha ha
Ok, I think I figured it out. Use
sudo npm install --unsafe-perm=true --allow-rootinstead@Lucinotion Do you think they would have any security concerns with granting node that level of access though? Granted it might be the only way for this set up to work smoothly, but essentially scripts running in node would now have full system level privilege wouldn't they? Probably a non-issue in this use case but just something to think of 🤷♂️
@skeddles In any case if the quoted solution works, awesome! Otherwise it looks like the npm cache is pointing at a location in that user home directory that doesn't exist for that user (the ".npm" folder).
npm cache clean --forcemight help reset that cache and then a cleannpm installinside the directory where your package.json is located may work/show more useful errors.Since node is installed at the right version now you should be right at the finish line here, just some ghosts of the journey to get here still causing bugs! Ha ha
He is right, the other solution would be to try to change all permissions and fix whatever permission mismatches are going on (search for info about EACCES), however now you should be able to do a sudo install.sh for the nvm (make sure that you can execute install.sh) or do a sudo bash install.sh , that should work too.
If you are afraid of security vulnerabilities, make sure that you can't execute something like this in node:
const { execSync } = require("child_process"); const veryDangerousExploit = execSync("sudo mkdir test");
If you can't and the test folder is not created, you should be good to go with node. You should also run this in node but without the sudo, and then make sure that the folder created is not owned by root. Just to make sure privilege scalation has not been somehow compromised.
I was able to get past npm install this time, but some modules were not listed in my package file still needed to be installed and gave me some errors. Do I need to run every npm install with sudo and --unsafe-perm=true --allow-root?
lospec@lospec-beta:~/htdocs$ npm install dotenv
npm ERR! code EACCES
npm ERR! syscall rename
npm ERR! path /home/lospec/htdocs/node_modules/abbrev
npm ERR! dest /home/lospec/htdocs/node_modules/.abbrev-TZU7hfxT
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, rename '/home/lospec/htdocs/node_modules/abbrev' -> '/home/lospec/htdocs/node_modules/.abbrev-TZU7hfxT'
npm ERR! [Error: EACCES: permission denied, rename '/home/lospec/htdocs/node_modules/abbrev' -> '/home/lospec/htdocs/node_modules/.abbrev-TZU7hfxT'] {
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'rename',
npm ERR! path: '/home/lospec/htdocs/node_modules/abbrev',
npm ERR! dest: '/home/lospec/htdocs/node_modules/.abbrev-TZU7hfxT'
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! /home/lospec/.npm/_logs/2021-02-09T00_05_16_006Z-debug.log
No, you shouldn't be running any command with sudo ever whatsoever.
The permissions on $HOME should always be recursively set so that whoami owns everything. In other words, try recursively chowning and chmoding $HOME.
@skeddles So for example you might run sudo chown -R $(whoami) $HOME to do what @ljharb suggested, so that you no longer need the sudo. (the -R flag indicates recursive, so it will apply to every file inside the $HOME directory, which should be the same directory you navigate to via cd ~)
@skeddles So for example you might run
sudo chown -R $(whoami) $HOMEto do what @ljharb suggested, so that you no longer need the sudo. (the -R flag indicates recursive, so it will apply to every file inside the $HOME directory, which should be the same directory you navigate to viacd ~)
This is the equivalent of doing sudo chown -R lospec /home/lospec
In case you need to change access modifiers, you can also do sudo chmod -R 755 $HOME where 755 should change to default values.
edit: just like the other command -R is for recursive
export NVM_DIR=$HOME/.nvm
after at you will be able to use:
calvez@docker-lara-prod:~$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 15037 100 15037 0 0 143k 0 --:--:-- --:--:-- --:--:-- 143k You have $NVM_DIR set to "/root/.nvm", but that directory does not exist. Check your profile files and environment. calvez@docker-lara-prod:~$ export NVM_DIR=$HOME/.nvm calvez@docker-lara-prod:~$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 15037 100 15037 0 0 164k 0 --:--:-- --:--:-- --:--:-- 164k => Downloading nvm from git to '/home/calvez/.nvm' => Cloning into '/home/calvez/.nvm'... remote: Enumerating objects: 355, done. remote: Counting objects: 100% (355/355), done. remote: Compressing objects: 100% (302/302), done. remote: Total 355 (delta 39), reused 168 (delta 28), pack-reused 0 Receiving objects: 100% (355/355), 208.50 KiB | 1.17 MiB/s, done. Resolving deltas: 100% (39/39), done.
- (HEAD detached at FETCH_HEAD) master => Compressing and cleaning up git repository
=> Appending nvm source string to /home/calvez/.bashrc => Appending bash_completion source string to /home/calvez/.bashrc => Close and reopen your terminal to start using nvm or run the following to use it now:
export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm [ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion" # This loads nvm bash_completion