NVM command works in terminal but not in screen - node.js

I am trying to have multiple connections to my dev server by ssh-ing once into my server and the using screen to open multiple sessions. My problem is that when I run nvm command in screen, the command can not be found. When I exit screen and run the command in the pure ssh terminal it runs fine. I think this is a PATH problem. I wanted to get an absolute path for nvm to see if I can run that in screen.
I ran the code below to find the path
~$ nvm
//SHOWS USAGE INFO
~$ whereis nvm
nvm:
~$ screen
//LOADS INTO SCREEN SESSION
~$ nvm
bash: nvm: command not found
What does this mean for the install of nvm. If I change its install directory could I then run it in screen.
If this is the case then why wouldn't screen run nvm in the first place.

Try manually activating nvm with:
source ~/.nvm/nvm.sh

Try
which nvm
instead of whereis. This will give you the full path.

On startup of a terminal I need to run .nvm.sh before I can execute nvm. I put .nvm.sh into my .bashrc. Turns out that an ssh connection runs the .bashrc but a screen connection does not. This is why the command was not available in screen. I had to manually run .nvm.sh.

Related

Facing "yarn not found" error while using terminal every time in mac

I want to use 'yarn' package manager to manage packages in my react-native project. I have successfully installed yarn package manager in my Mac by this command "npm install --global yarn" and set the env path by this command export PATH=/Users/admin/.npm-global/bin:$PATH.
All good I am able to use yarn after that.
The problem is when I again try to open terminal later than again it shows the "bash: yarn: command not found" and I need to set env path.
So how I set path permanently in my Mac, so that later I don't need to set env path again and again.
Anyone please help me.
You need to add export PATH=/Users/admin/.npm-global/bin:$PATH to your .bashrc file, in your home directory:
cat 'export PATH="/Users/admin/.npm-global/bin:$PATH"' >> ~/.bashrc
The content of the .bashrc file is run each time bash is started. (except maybe for login shells. In the case of a login shell, I believe .profile is run instead)

Running a bash script (with some npm commands in it) in Ubuntu by double-clicking on the bash-file runs into `npm: command not found` error

I am currently trying to set up my system so I can run my bash file (startWebApp.bash) on Ubuntu by double-clicking on it. Unfortunately, that is not working, but when I run the script in the terminal with ./startWebApp.bash it works fine.
The underlying problem seems to be that the $PATH variable is different when running the script by double-clicking on the file. The $PATH variable when I run the file in my Terminal is:
/home/magonba/.nvm/versions/node/v14.17.6/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
While when I run the file by double-clicking on it, $PATH is:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
Why is there a difference and how can I change that?
Reproduction steps:
Create the file startWebApp.bash with the following content:
#!/bin/bash
sudo service postgresql restart
cd /home/my/project/folder/nodejsapp
npm start &
cd /home/my/project/folder/vuejsapp
npm run serve &
I set in the File Manager under Preferences -> Behavior -> Executable Text Files to Ask what to do (in order to be able to start the application by double-clicking on it).
When I double-click on the file, Ubuntu offers me the options: Run in Terminal, Display, Cancel and Run
I choose Run in Terminal (because I need to provide a password since I am using a sudo command)
Runs into the error(s):
/home/my/project/folder/nodejsapp/startWebApp.bash: line 6: npm: command not found
/home/my/project/folder/vuejsapp/startWebApp.bash: line 8: npm: command not found
Thanks for any help in advance!
The problem you got here is, that nvm is integrated in you .bashrc. But when you run your bash file from the GUI, your bashrc will be ignored, and a very minimal bash shell is started. You can fix this in multiple ways.
Install npm to a system installation path (e.g. with apt)
Change your $PATH in the script
#!/bin/bash
PATH="/home/magonba/.nvm/versions/node/v14.17.6/bin:$PATH"
sudo service postgresql restart
cd /home/my/project/folder/nodejsapp
npm start &
cd /home/my/project/folder/vuejsapp
npm run serve &
Every time you call npm, give the full path
#!/bin/bash
sudo service postgresql restart
cd /home/my/project/folder/nodejsapp
/home/magonba/.nvm/versions/node/v14.17.6/bin/npm start &
cd /home/my/project/folder/vuejsapp
/home/magonba/.nvm/versions/node/v14.17.6/bin/npm run serve &
Include your .bashrc in your script
#!/bin/bash
. /home/magonba/.bashrc
sudo service postgresql restart
cd /home/my/project/folder/nodejsapp
npm start &
cd /home/my/project/folder/vuejsapp
npm run serve &
Please note.
If you go for solution 2 or 3, every time you change your npm version via nvm, you will have to change your script accordingly.

Cannot run NPM Commands

I've been using NPM on my machine (Windows 10), but recently ran into an issue. I currently have Node.js installed and get the following error while running any npm command.
Question: What is causing this error and whats the best way to resolve it.
Command:
$ npm install
Output/Error:
bash: /c/Program Files/nodejs/npm: /bin/sh^M: bad interpreter: No such file or directory
Not my solution, but this seemed to work for me. Appears that having the Windows folder structure in $PATH while using WSL2 was causing that parse error, but I'm not exactly sure why.
Go to your user root (cd ~)
Open .bashrc in your chosen editor (vi, nano, etc.)
Append to the end of the file: export PATH=$(echo "$PATH" | sed -e 's/:\/mnt[^:]*//g') # strip out problematic Windows %PATH%
Close and re-open all terminal windows
Source: https://hackmd.io/#badging/wsl2#Troubleshooting-PATH
Updated: Per Lh Lee's comment, I've updated the regex from s/:\/mnt.*//g to s/:\/mnt[^:]*//g as this avoids accidentally capturing anything extra after the problematic /mnt paths.
Whereas the first regex will match /mnt/c/blah:/other/thing, the new one will not.
Install nodejs/npm using nvm and it will not conflict with the one in windows.
The path for npm becomes (after installing using nvm) /home/ubuntu/.nvm/versions/node/v14.16.0/bin/npm
Read More about setting up your Node.js development environment with WSL 2
I did with these commands.
sudo apt update && sudo apt install curl -y
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
source ~/.profile
nvm ls-remote
nvm install v15
node --version
This command with 78 upvotes:
# export PATH=$(echo "$PATH" | sed -e 's/:\/mnt[^:]*//g') # strip out problematic Windows %PATH%
causes problems with other commands such as the "code" command for VS Code. It manipulates the $PATH. The correct solution here is to use nvm:
sudo apt-get install curl
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
nvm ls
nvm use 14
I had the same issue, I solved it by installing my dependencies on my Linux sub system as well. In my case it was just nodejs and npm.
Open Ubuntu terminal(Windows Sub system) I'm on windows 10...
sudo apt update
Now that the system is up to speed, let's do...
sudo apt install nodejs npm
(Do a variation of this suitable to you, I assume most are using nodejs though.)
My npm works now but still gives the error first. I mean it's not aesthetic at first, but my terminal on VS Code doesn't have the error first. Which is awesome!
TLDR: The linux subsystem doesn't have npm so download it there first.
Edit: formatting
It happened to me just now and all I had to do was exiting my wsl2 session and login in again.
Just faced the same issue, this issue happens because npm is installed on your windows machine but not on your WSL one. you just need to install npm on your linux machine then it will read the binary from linux not windows that's in case you want to use windows paths on your WSL. otherwise if you don't need the windows paths you can use thing adam mentioned by adding this into your path: PATH=$(echo "$PATH" | sed -e 's/:\/mnt.*//g') then refresh the shell with source ~/.bashrc
I encountered the same problem and found the cause and a simple solution.
After installing nvm in bash a few months ago, I recently decided to give zsh and on-my-zsh a try. I followed the instructions and installed zsh and oh-my-zsh. When trying to run node or npm I got the errror:
zsh: /mnt/c/Program Files/nodejs/npm: bad interpreter: /bin/sh^M: no such file or directory
My investigations led me to the $PATH variable. I then compared the output of echo $PATH in bash and zsh. In bash the path included the nvm directory, in zsh this directory was not added to the path.
The reason for this difference is that nvm adds a snippet to the end of .bashrc. In zsh .zshrc is loaded instead and the snippet will not be executed.
The snippet looks like this:
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
You can just copy these lines from .bashrc to the end of .zshrc, restart the shell, and the issue should be fixed if you have the same problem.
If you already installed everything you need following this link https://learn.microsoft.com/en-us/windows/dev-environment/javascript/nodejs-on-wsl
just try restarting your VS Code and trying to run npm install.
I got this error when running npm install in a Visual Studio Code terminal, using Powershell. I switched to use a WSL2 terminal inside Visual Studio Code instead and it worked (tip - use npm i as a shorthand)
I am using Git Bash for cli
I my case my Antivirus has quarantined the C:\Program Files\Git\user\bin\sh.exe file, and that is why $ npm init was not working and showing this error:
bash: /c/Program Files/nodejs/npm: /bin/sh^M: bad interpreter: No such file or directory
When I restored that file it started working normally.
experienced this problem on my wsl2
not a fix but a workaround. unmounting the c drive resolve the issue.
umount /mnt/c
I had the same problem when I started using WSL, I solved it by using npm.cmd instead of npm command.

"nvm" command not recognized in new command line windows/tabs despite presence of "NVM_DIR" variable in both ~/.bashrc and ~/.bash_profile

My colleague recently installed Node Version Manager on his Macbook using Homebrew, and ran the two commands suggested at the end of the install script:
export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh
Everything works fine in the terminal window in which the install took place, but if he opens up a new terminal window or even a new tab, he has to install NVM all over again.
My level of command-line experience is relatively low (I don't know what I don't know), so I'm not sure where to start diagnosing the problem. Suggestions are welcome.
The instructions from brew after installing nvm are:
Add the following to $HOME/.bashrc, $HOME/.zshrc, or your shell's equivalent configuration file:
source $(brew --prefix nvm)/nvm.sh
On OS X with default settings, you actually want $HOME/.profile (or ~/.profile). Just add the line above to the end of that file.
Running that line once will set up nvm in that shell session. But if you add it to your .profile file, it will be run at the beginning of every shell session.
If you don't want to edit the .bash_profile
You could also export the nvm() function to your current shell enviroment:
source $(brew --prefix nvm)/nvm.sh
to check that is working:
nvm -v
If you want to edit the .bash_profile
sudo vim ~/.bash_profile
Type i to insert, and add:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
Press wq! to save and exit and finally restart the terminal application.
Never Give Up! ✌️

Managing multiple Node.js versions with nvm in a tmux session

Short question
How can I get nvm to work with tmux?
Path Problems
As MarkHu surmised in his answer, the problem is related to the PATH. Below is a comparison of the path in bash before entering tmux and after starting tmux. (I replaced each : in the path with a line break to make it easier to compare.)
Now I'm curious as to:
Why are /usr/local/sbin and /Users/matthew/bin are duplicated in the path?
Why did /Users/matthew/.nvm/v0.11.5/bin along with /usr/local/sbin and /Users/matthew/bin get moved to the end of the path?
Background
I'm running OS X 10.8.4 Mountain Lion. I installed tmux v1.8 and Node.js using:
brew install tmux
brew install node
I then decided I wanted to manage multiple node version, so I installed nvm using:
curl https://raw.github.com/creationix/nvm/master/install.sh | sh
If I'm not in a tmux session, nvm appears to work correctly. When I start a tmux session though, it finds the Node.js installed by Homebrew instead of using the nvm version. Any thoughts on how to get nvm to work with tmux?
$ which node
/Users/matthew/.nvm/v0.11.5/bin/node
$ tmux
$ which node
/usr/local/bin/node
$ nvm use v0.11.5
Now using node v0.11.5
$ which node
/usr/local/bin/node
I just run:
nvm deactivate
nvm use x.x
That seems to work. You can script out the needed version depending on the project you're working on.
Check your $PATH environment var before and after.
There is a difference between setting it in ~/.bash_profile and ~/.bashrc depending on how you have those configured.
Also, you may want to read https://unix.stackexchange.com/questions/15453/using-environment-variables-in-tmux-conf-files if you think other node.js-related vars may need to be set.

Resources