8

When trying to deploy a Node.js application I'm getting an error npm: command not found in my post_install.sh AfterInstall script. What's super strange is in my BeforeInstall script I run npm install -g pm2 and it works perfectly fine without any errors.

Both are being run as the ubuntu user. Why would this command work in one CodeDeploy script and fail in another?

3
  • Add the full path tot the npm executable. Commented Sep 5, 2017 at 11:17
  • @RamanSailopal How can I find that? And why exactly do I have to do that? Commented Sep 5, 2017 at 16:51
  • Type "whereis npm" at the command line and then in the script, add the full path as opposed to just npm. Commented Sep 6, 2017 at 10:47

3 Answers 3

22

Looks like I was able to solve this by adding the following to my script files.

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

Hopefully this can help someone.

Sign up to request clarification or add additional context in comments.

Comments

0

I got the same problem trying to invoke the aws cli in my shell script during the AfterInstall hook, in an ubuntu server with ubuntu user.

This is what happened when I executed my script manually sh ecr-login.sh:

# Works
$(aws ecr get-login --no-include-email --region sa-east-1)

# Works
$(/home/ubuntu/.local/bin/aws ecr get-login --no-include-email --region sa-east-1)

When CodeDeploy executed it:

# Not Work
$(aws ecr get-login --no-include-email --region sa-east-1)

# Works
$(/home/ubuntu/.local/bin/aws ecr get-login --no-include-email --region sa-east-1)

Comments

0

I got the same problem trying to invoke the aws cli in my shell script during the After Install hook, in an ubuntu20 server with ubuntu user.

I have checked inside /usr/bin/ folder on server yarn/pm2/node are running are not If not run below command root@ip-:/# ln -s /root/.nvm/versions/node/v16.17.0/bin/node /usr/bin/ root@ip-:/# ln -s /root/.nvm/versions/node/v16.17.0/bin/pm2 /usr/bin/ root@ip-:/# ln -s /root/.nvm/versions/node/v16.17.0/bin/yarn /usr/bin/

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.