I am using capistrano to deploy a node.js application, and have a
problem with setting the shell tasks.
For instance, thought I have npm installed this fails:
run "npm install"
npm not found
and when I use
run "/opt/nvm/'cat /opt/nvm/alias/default'/bin/npm install"
the error is node not found
The deploy is managed by a special user for deploy.
Could you please tell what might cause this problem and how to solve it?
Using NVM and Capistrano is working for me by running the command through bash and sourcing the nvm.sh file first.
My NVM is installed in /opt/nvm, so the npm install task could look something like this:
namespace :npm do
desc 'Install the current npm environment.'
task :install do
invoke_command "bash -c '. /opt/nvm/nvm.sh && cd #{current_path} && npm install'", :via => run_method
end
end
So no need of manually setting the path to the binaries by reading the alias file from NVM.
Sounds like the npm/node executables are not on the $PATH for the remote user that is executing the Capistrano script.
You should double check which user Capistrano is running as and what the $PATH is (and correcting as required)
I ended up adding this to my Capfile
set :default_environment, {
'PATH' => "$PATH:/usr/local/ruby/bin/:/home/ubuntu/.nvm/v0.10.21/bin"
}
In the meantime (more than a year back tho) I've created a Capistrano extension for easy nvm usage: https://github.com/koenpunt/capistrano-nvm
The extension will map node and npm by default, but you can add any executable that needs nvm to run to it (eg. grunt).
A basic setup would work by adding the following to your deploy.rb:
require 'capistrano/nvm'
set :nvm_type, :user # or :system, depends on your nvm setup
set :nvm_node, 'v0.10.21'
set :nvm_map_bins, %w{node npm}
I was able to affect node version changes by adding lines like the following to my capistrano deploy scripts:
execute :'. ~/.nvm/nvm.sh && nvm use vOLDER_VERSION && nvm alias default vOLDER_VERSION'
execute :npm, 'install', '--silent --no-progress'
execute :npm, 'run', 'build'
execute :'. ~/.nvm/nvm.sh && nvm use stable && nvm alias default stable'
Where vOLDER_VERSION is the version of choice.
The way this works is it sources nvm, sets it to use the specified version, and sets that version as default. It then reverts those changes after the npm steps have been run. This way other parts of the deploy or other deploys still get to use the latest version.
If you don't care about other deploys you could just run that step once for your deployer user:
su deployerUser
nvm use vOLDER_VERSION
nvm alias default vOLDER_VERSION
Related
My Problem
I have installed fnm (Fast Node Manager) from this github repo and it works all great except for installing global npm packages. For example, the well-known package nodemon is something I want installed globally and not im my node_modules project directory.
When installing the package globally there seems to be no problem:
And when checking the global package list, nodemon seems to be there:
But when running the command nodemon I get the following output:
As also seen in the fnm repository documentation there is a need to run this piece of code eval "$(fnm env --use-on-cd)"; on load in order to get fnm to work properly and this is what I have done in the .bashrc file.
Note
I am using windows 10, seems to be working on my mac laptop.
The Question
How can I have a global npm package installed for all or at least a single fnm node version? And what I mean by this, is that by running fnm use <NODE_VERION> you specify what node version to use as also seen in the repository documentation. I want to be able to run the nodemon command without it being installed in a project's node_modules directory.
You do not need to delete the multishells. The problem is the Git Bash path.
Fix is here: https://github.com/Schniz/fnm/issues/390
Put this in your .bashrc
eval $(fnm env | sed 1d)
export PATH=$(cygpath $FNM_MULTISHELL_PATH):$PATH
if [[ -f .node-version || -f .nvmrc ]]; then
fnm use
fi
As mentioned this actually worked on my OS X machine (aka my mac book pro) but not on my windows 10 computer. The solution I came up with after analyzing thoroughly the behaviour of fnm is the following:
Go to C:\Users\<YOUR_USER>\AppData\Local\fnm_multishells and delete the directory if it exists.
When downloading global packages do it via CMD or any terminal which isn't bash (or the terminal that has the "$(fnm env --use-on-cd)"; script) as this makes fnm then search for the global package in the wrong place.
This approach mitigates any path errors as I found that this was the core problem. As shown in the screenshot above when trying to run nodemon it looks for it in C:\Program Files\Git\Users\Valeri..... but this directory simply does not exist. After removing the directory mentioned in step 1 fnm stops looking for nodemon in that path and instead uses the one installed via CMD.
Essentially, the "$(fnm env --use-on-cd)"; script allows us to use fnm properly but at the same time causes this issue. Simply download global npm packages from a terminal that does not run this command.
Edit
I just had the same issue and to confirm you don't even need to delete the fnm_multishells directory. Just run npm -g remove <whatever> and install it via cmd or powershell. A command-line which does not run "$(fnm env --use-on-cd)"; on load.
Suddently I can't install angular
I get stuck at the npm install command
"npm install -g #angular/cli"
It stays forever on this "checking installable status".
my node version is 8.11.3 (yes I already tried to uninstall node and double checked to see it was really uninstalled) this was the version I had before and was working fine
my npm -v gives 5.6.0
then I run the angular command to install angular and it seems to freezing or very very very slow....
npm install -g #angular/cli --verbose
After running this command I realized npm was having problems with the connection with registry.npmjs.org
To solve this:
npm config set registry "http://registry.npmjs.org"
npm set maxsockets 3
Viewed here
In my case I had to wait for a few minutes and npm finally installed by package.
Also I suggest to use --verbose flag to see what's actually is going.
Try this:
npm install -g --no-optional pm2
This will ignore all the dependencies specified in the package.json file (if present).
Hope this helps.
To anyone still experiencing this, I spent days searching for a solution, it ended up being easier and more effective to just remove all traces of nvm (and its node) from my machine and reinstall. Everything started working smooth again after the reinstall
I did:
brew uninstall nvm
rm -rf $NVM_DIR ~/.nvm ~/.npm ~/.bower
# remove nvm entries from my .bash_profile|.bashrc then
# installed nvm from nvm's install script
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.0/install.sh | bash
# add to bash_profile
cat << EOF >> ~/.bash_profile
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
EOF
source ~/.bash_profile
nvm install --lts
Try npm install --g --no-optional pm2
Check this out for reference
You may need to specify your proxy server in the global Git configuration, like this:
git config --global http.proxy http://your-proxy-server:port
git config --global https.proxy http://your-proxy-server:port
And since you mentioned you're using Git for Windows, best to put this into the system wide configuration, too (repeat these commands with --system instead of --global).
Theoretically, the global config should take precedence over the system config, but sometimes when using npm install on Windows, the global config seems to be ignored or not found. I suspect this can happen when there are conflicting settings in the USERPROFILE and HOMESHARE env vars, in which case Git may be confused and look in different places depending on how it is invoked.
It is possible that you have a custom registry set up in your global .npmrc. That was the problem in my case: my company uses a custom registry which falls back to the NPM registry. It is not a problem for work projects, because all the required packages are already present there, but I didn't realize this was affecting a new project whose packages were not included in the mirror registry and looking them all up must have been the cause of the slowdown.
I solved it by resetting the registry config to the NPM registry in an .npmrc for that specific project:
registry=https://registry.npmjs.org
For me the issue is that the package I was trying to install had this in its package.json:
"dependencies": {
[...]
"mobx-utils": "github:Venryx/mobx-utils#5.5.2_VPatch2"
}
Normally that works fine, but apparently today, NPM decided to hang on the call to retrieve the library contents from the GitHub repo.
I used Process Hacker 2 to investigate what exact command was hanging, and it was the following:
git.exe ls-remote -h -t git://github.com/Venryx/mobx-utils.git
In my case, I worked around the issue by just manually installing the subdependencies (and copy-pasting the mobx-utils library itself), but this is of course not ideal.
UPDATE: The issue is that I was running an outdated version of Git for Windows. Once I updated it to the latest version (v2.28.0), the issue was resolved. (ie. installing based on github urls/branches started working just fine again)
Or maybe there is a service outage as in my case. Recommending to first check the status page, where you get the info, if everything is working as it should be: https://status.npmjs.org/
If you see something like this, you know, you will have to wait for a while:
for those with the same problem try
npm config set strict-ssl false
I'm running a nodejs application's build on jenkins. I run node as shell script step, because I have some limitations in terms of the plugins I can install in this jenkins instance.
This is what the npm install step looks like:
#!/bin/bash +x
source ~/.bashrc
cd $WORKSPACE/ && \
nvm use 7.8.0 && node --version && npm install
The problem I have is, when npm install finishes, it doesn't install everything. If I ssh into the box where jenkins is installed and run npm install inside that project's workspace, with the same user jenkins uses, it works ok. Any ideas?
EDIT
I just realized the dependencies that it's not installing are devDependencies
The problem was I had the node env set to production, and of course, it wouldn't get the devDependencies...
I have this post-receive script as a git hook when exporting with the following contents
#!/usr/bin/env bash
export NODE_ENV=production
git --work-tree=/home/myusername/app --git-dir=/home/myusername/git checkout -f
cd /home/myusername/app
npm prune
npm install --production
knex migrate:latest
When ssh'd in my server I installed knex globally but it doesn't seem to exist within the bash shell's environment. I lack the knowledge of knowing how they are different. I also noticed my node versions were different. How do I import my user's normal environment?
To get the names and versions of modules that you have installed globally run:
npm list --global --depth=0
You can then install those modules on the second system with:
npm install --global module_name module_name ...
If you want to install a specific version of Node on the second system you can use nvm or see those answers for details:
Run npm as superuser, it isn't a good idea?
node 5.5.0 already installed but node -v fetches with "v4.2.1" on OS X & homebrew?
Node installed but node cannot be found in Ubuntu VPS
This is the first time I see a shebang like that:
#!/usr/bin/env bash
I've seen using /usr/bin/env to find node, python, perl interpreters etc. but never for bash. Usually it's just:
#!/bin/bash
Remember that if you want to run some program from a Bash script (without giving a full path) then it must be in a directory that is in the PATH environment variable.
To see your PATH run:
echo $PATH
Or you can use a full path to a binary in a script:
#!/bin/sh
/full/path/to/something
and then it doesn't need to be in the PATH.
I am trying to downgrade my version of node
I ran:
npm install nvm
and I exported the bin folder to my Windows path variable,
C:\Program Files (x86)\nodejs\node_modules\npm\bin
but I still get:
'nvm' is not recognized as a an internal or external command.
Should I be adding another path to my path variable?
nvm was designed for Linux. nvmw, which is completely different, broke around node v0.10.30. Try NVM for Windows.
NVM can be used to manage various node version :
Step1: Download
NVM for Windows
Step2: Choose nvm-setup.zip
Step3: Unzip & click on installer.
Step4: Check if nvm properly installed, In new command prompt type nvm
Step5: Install node js using nvm :
nvm install <version> : The version can be a node.js version or "latest" for the latest stable version
Step6: check node version - node -v
Step7(Optional)If you want to install another version of node js - Use STEP 5 with different version.
Step8: Check list node js version - nvm list
Step9: If you want to use specific node version do - nvm use <version>
NVM Installation & usage on Windows
Below are the steps for NVM Installation on Windows:
NVM stands for node version manager, which will help to switch between node versions while also allowing to work with multiple npm versions.
Install nvm setup.
Use command nvm list to check list of installed node versions.
Example: Type nvm use 6.9.3 to switch versions.
For more info
As an node manager alternative you can use Volta from LinkedIn.
I created a universal nvm that works on both Unix (bash) and Windows, base on another simple nvm.
It doesn't need admin on Windows, but requires PowerShell 4+ and the right to execute scripts.
https://www.npmjs.com/package/#jchip/nvm#installation
The first thing that we need to do is install NVM.
Uninstall existing version of node since we won’t be using it anymore
Delete any existing nodejs installation directories. e.g. “C:\Program Files\nodejs”) that might remain. NVM’s generated symlink will not overwrite an existing (even empty) installation directory.
Delete the npm install directory at C:\Users[Your User]\AppData\Roaming\npm
We are now ready to install nvm. Download the installer from https://github.com/coreybutler/nvm/releases
To upgrade, run the new installer. It will safely overwrite the files it needs to update without touching your node.js installations. Make sure you use the same installation and symlink folder. If you originally installed to the default locations, you just need to click “next” on each window until it finishes.
Credits
Directly copied from : https://digitaldrummerj.me/windows-running-multiple-versions-of-node/
I will list two ways. You can choose one Whichever works for you.
1. Using installer
Download nvm-setup.zip and unzip the file and install it, keeping the configurations default.
1. Use curl
Copy the below command and run it in your terminal
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
After this reopen/open terminal and check the nvm version runing below command.
nvm -v
And that's it.
If someone is looking for install on Window 11! Not directly relevant here, but might be useful.
It is immaterial if you install NVM (version 1.1.9.) say after the node (16.15.1) is already installed. During the nvm installation process, it asks for the right to manage the existing node version and symlinks that.
Get the version from the GitHub repo, I opted for the zip version.
https://github.com/coreybutler/nvm-windows/releases
Double click the application and it is just a few steps.
1.downlad nvm
2.install chocolatey
3.change C:\Program Files\node to C:\Program Files\nodejsx
emphasized textThe first thing that we need to do is install NVM.
website :
https://learn.microsoft.com/en-us/windows/nodejs/setup-on-windows
So this answer is for windows users that are using git bash or some other console emulator like cmder ... if you're using CMD this solution will not work for you also why? why are you still using CMD?
I know this is a pretty old post but I just achieved this yesterday and wanted to add my answer for anyone looking to do the same.
First check if you have .bashrc profile in your home directory by typing ls -alh ~ (by default this doesn't exist)
if it doesn't exist type this command to generate a .bashrc profile with default values in it cat /etc/bash.bashrc > ~/.bashrc (if it does exist skip this step)
Download and run the nvm install script as provided in the nvm docs page curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash (make sure you do this in your home directory)
then edit the new generated .bashrc profile file you created above; use nano/vim to do that nano ~/.bashrc and add the following to the bottom of the file export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm and save your .bashrc file with the changes.
lastly source your .bashrc file by typing source ~/.bashrc
verify installation nvm --version
and now you have nvm installed and you can use the commands as per https://github.com/nvm-sh/nvm#usage
First off, I use nvm on linux machine.
When looking at the documentation for nvm at https://www.npmjs.org/package/nvm, it recommendations that you install nvm globally using the -g switch.
npm install -g nvm
Also there is a . in the path variable that they recommend.
export PATH=./node_modules/.bin:$PATH
so maybe your path should be
C:\Program Files (x86)\nodejs\node_modules\npm\\.bin
An alternative to nvm-windows, which is mentioned in other answers would be Nodist.
I've had some issues with nvm-windows and admin privileges, which Nodist doesn't seem to have.
I know I'm late here but this may help in the future if someone looking for NVM to install in Windows or linux
run this command in cmd
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash