Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Github Actions and npm - npm: command not found

I've created a action for a deployment on github actions. This all works with composer install and git pulling the master branch. However on my digital ocean droplet, I get the issue bash: line 4: npm: command not found If i ssh into my server i can use npm perfectly fine. This was installed via nvm and uses the latest version but for some reason its not accessable via the action. My deployment script is


on:
  push:
    branches: [master]
jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Deploy Laravel APP
        uses: appleboy/[email protected]
        with:
          host: ${{secrets.SSH_HOST}} 
          key: ${{secrets.SSH_KEY}} 
          username: ${{ secrets.SSH_USER }} 

          script: |
            cd /var/www/admin
            git pull origin master
            composer install
            npm install
            npm run prod

I presume this is more to do with the setup from nvm as i can use this via ssh but as they use the same user to log in via ssh, i can't seem to see an issue.

Any ideas how I can resolve this issue to give access/allow github actions to use npm?

like image 357
Ashler2 Avatar asked Oct 18 '25 11:10

Ashler2


2 Answers

I had the same issue, and finally found the solution. I could solve the issue by adding the following lines before running npm commands.

export NVM_DIR=~/.nvm
source ~/.nvm/nvm.sh

These commands helps the terminal fix the node path installed by nvm. Reference link is here.

like image 90
Bi Wu Avatar answered Oct 20 '25 01:10

Bi Wu


The answer provided by @iconique is correct. I just had to tweak it a bit to point to the correct location of .nvm.sh

To see the exact location of your .nvm.sh, you need to run

whereis pm2

In my case, I updated the .yml file as follows:

jobs:
  build:
    name: Build & Deploy
    runs-on: ubuntu-latest
    steps:
      - name: Deploy to Production
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.SSH_HOST }}
          username: ${{ secrets.SSH_USERNAME }}
          password: ${{ secrets.SSH_PASSWORD }}
          port: ${{ secrets.SSH_PORT }}
          script: |
            export NVM_DIR=/home/${{ secrets.SSH_USERNAME }}/.nvm
            source /home/${{ secrets.SSH_USERNAME }}/.nvm/nvm.sh
            git stash
            git pull origin main
            npm i
            npm run build
like image 44
daniel mburu Avatar answered Oct 20 '25 00:10

daniel mburu