Running npm install on Ubuntu with Octopus Deploy - node.js

We are using Octopus deploy to deploy an angularjs app. I'm running a post deployment script (bash) and in there I try to do npm install. This doesn't work, I get an error
npm: command not found
However if I login to the linux box as the Octopus user, go to the directory that Octopus Deploy is trying to run the script from, I am able to run npm install without error.
I've confirmed its the right user (running whoami before npm install in the post deployment script).
I've tried adding the npm executable to my PATH variables which didn't work. I've also put the full path to npm in my script which gives me a new error
/usr/bin/env: ‘node’: No such file or directory

Please run the following command as octopus user to access npm for other users.
n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local
Or use nvm to install node
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
source ~/.bashrc
source ~/.profile
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  
nvm ls-remote
nvm install 8.10.0 
node --version
npm --version
which node
n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local
https://github.com/creationix/nvm
If you are installing new node version, please do the following steps.
nvm list will show all installed node versions
nvm use v8.10.0
nvm alias default v8.10.0 set default node version for current user

Related

Why installed Node.js and Yarn is lost after reconnect to EC2 linux instance

I have on AWS EC2 instance. When I install Node.js with this
AWS tutorial all works fine until i logout current session and log in, than Node.js and Yarn are gone. There is also note from AWS:
The node installation only applies to the current Amazon EC2 session. If you restart your CLI session you need to use nvm to enable the installed node version. If the instance is terminated, you need to install node again. The alternative is to make an Amazon Machine Image (AMI) of the Amazon EC2 instance once you have the configuration that you want to keep, as described in the following topic.
Is it possible to install Node.js so it is available in another session? I installed zsh and that works for all sessions.
I also created simple script that will install Node.js and Yarn. Commands in this script are from AWS Tutorial page that I mentioned early. It will install Node.js and Yarn, show that all works (it shows Yarn version and message from node command), but when I type npm --version or yarn --version it will shows message zsh: command not found: yarn (or similar message for npm).
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
. ~/.nvm/nvm.sh
nvm install --lts
node -e "console.log('Running Node.js ' + process.version)"
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
echo "installing YARN !!!!! "
npm install yarn --global
Why is my script not working?
If you use manual from AWS how to install Node.js than instalation is only made in your home directory that lives only for current session. To install Node.js and Yarn I used this post: https://stackoverflow.com/a/35165401/78935
Or you could simply use this:
sudo curl --silent --location https://rpm.nodesource.com/setup_16.x | bash
sudo yum -y install nodejs
sudo npm install yarn --global
yarn --version

NVM managed npm globally installed packages return command not found

I have nvm installed to manage my node versions. If I install a package globally, npm install -g fkill, then run fkill, I get the error zsh: command not found: fkill.
Here are some commands run to help with debugging this
$ npm root -g
/home/jchi/.nvm/versions/node/v10.15.3/lib/node_modules
Looking at my PATH...
$ echo $PATH
/home/jchi/.pyenv/shims:/home/jchi/.pyenv/bin:/home/jchi/.nix-profile/bin:/home/jchi/.autojump/bin:/home/jchi/.nvm/versions/node/v10.15.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/games
I do not see the output of npm root -g in my PATH.
I assume is nvm's responsibility to add that to my path so globally installed packages can be run. So I look at what I have in my .zshrc that kickstarts nvm.
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
105 [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
Suggestions appreciated
Looking at your PATH, you do have the nvm bin folder under your PATH.
/home/jchi/.nvm/versions/node/v10.15.3/bin
I would suggest to check if you have prefix related settings in ~/.npmrc.
If you have any prefix settings, please remove it.
Then try running npm install -g fkill again.
One way to fix this is to add your NVM node bin to your path.
What node version are you using? node --version
Add the line below to your .bashrc (replace VERSION with what you got in step 1)
export PATH="$HOME/.nvm/versions/node/VERSION/bin:$PATH"
Example:
export PATH="$HOME/.nvm/versions/node/v16.1.0/bin:$PATH"
When you refresh your terminal (. ~/.bashrc or re-opening terminal), your global Node commands should work now.
Unfortunately, it seems this needs to be done for each version of Node you use.
In my case, I had a package (let's call it cat-hat) installed globally on Node 14 using NVM (verified by checking .nvm/versions/node/v14.17.6/lib/node_modules/cat-hat) but I was getting zsh: command not found when trying to run it in my command line.
The issue in my case was that cat-hat only runs OK on node versions below 13. This can be verified in the package.json engines.node field.
I fixed this by changing to node 12, and installing cat-hat again for v12, and it worked as expected.

Cant fix npm permissions mac mojave

I have spent the last few hours trying every tutorial out there on how to fix npm permissions on a mac.
NOTHING has worked thus far.
Steps I have taken
I have uninstalled node multiple times, tried running brew install node --without-npm then installing npm seperately.
I have tried to create my own npm-package file in my root directory and change the npm source and still that doesnt work.
I have tried what is detailed here https://github.com/sindresorhus/guides/blob/master/npm-global-without-sudo.md
I have tried what is shown in this video https://www.youtube.com/watch?v=bxvybxYFq2o&t=154s
I have also tried a few other techniques all similar to above but nothing has worked.
Im on a 2015 macbook running a fresh install 0SX Mojave.
Any time I try to install a package it errors, and even if I use sudo, npm cant find the commands I'm entering for example if I
sudo npm install #angular/cli then try and use ng I get the command not found: ng
Any help would be appreciated. This question will be updated with methods used as they are suggested. Let me know If you need any more information.
I understand your frustration :(
These steps worked for me (from https://github.com/creationix/nvm/blob/master/README.md):
check if you have ~/.bash_profile file (home directory). I didn't. So I had to create it myself with:
touch ~/.bash_profile
Get NVM by running:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
them run
export NVM_DIR="${XDG_CONFIG_HOME/:-$HOME/.}nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
verify nvm installation by running (you might need to restart terminal window before)
nvm --version
Install node via nvm - run:
nvm install node
(if you get version warning/error just follow the instructions(I had to run npm config delete prefix and then again nvm install node))
Install CLI:
npm install -g #angular/cli
verify installation:
ng -v
Hope it helps!

Jenkins is not installing some node dependencies

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

Problem with making Capistrano run the shell tasks (nodejs deploying)

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

Resources