GitLab CI/CD - npm not found

Hello,
I have create gitlab ci/cd script, but script gets terminate with error : npm command not found (npm already installed globally)
if I run commands manually on server then its works perfectly.

Hi @dimpal_rana

Are you using GitLab SASS and are you using your own runners or GitLab runners? Also, would it be possible for you to post your .gitlab-ci.yml file?

we are using our own gitlab hosted and using gitlab-runner locally.
image

OK, so looking through this, it seems to me that your vuejs.sh script is running on your host server, in a “shell executor” – i.e. it’s running on the server, not inside it’s own Docker image.

In that case, you need to install npm on the server, or ask your server admin to do that.

If I’ve misunderstood, and you are running this in a Docker container, then one thing you could do is just ensure that the script runs in a container that already has npm installed, like this:

deploy-stagging-magento:
    stage: deploy
    image: npm:latest
    ....

Does that help?

We have already installed npm on host server
command run successfully on server if I run it manually
but fail from CI/CD script

OK, so that gives you a lot of clues about what might be happening! So, if this were me, and I had access to the server, I would want to run which npm and find out where that command is stored and who has access to it. I’d also want to find out whether it’s on the path of the gitlab-runner user.

If I didn’t have shell access to the server, I’d add some commands to .gitlab-ci.yml like this:

deploy-stagging-magento:
    stage: deploy
    before_script:
        - whoami
        - pwd
        - locate npm
        - echo ${PATH}
        - which npm
        - ls -lh $(which npm)
...

and hopefully that should give you a clearer idea of where the problem is.

From gitlab-runner

and getting proper version of pm2 from direct server.
can’t understand why its showing no command found on gitlab-runner.

Can you run which pm2 directly on the server?

getting this on which pm2
/home/ubuntu/.nvm/versions/node/v10.16.3/bin/pm2

Aha! So that’s your issue. If you look at the output from the CI, that directory isn’t on the PATH of the gitlab-runner user. You can add it permanently in a ~/.bashrc or ~/.bash_profile file, or add export PATH=/home/ubuntu/.nvm/versions/node/v10.16.3/bin/:$PATH in the relevant job in your .gitlab-ci.yml file.

after adding path still same issue

@snim2 would you please help me with this?

Where does that sudo come from? Is the call to pm2 really being run by the gitlab-runner user?

going to reinstall pm2 and npm globally
may be that can help

@snim2 thanks for the path suggestion after reinstalling npm and pm2 in usr/local/bin
it’s working perfectlly :slight_smile:

1 Like

I have the same problem.

Here’s my .yaml file:
stages:

  • setup
  • install
  • build
  • deploy

variables:
NVM_DIR: “/home/riley45-sc/.nvm”
NODE_VERSION: “node:18-alpine”

setup:
stage: setup
script:
- microdnf install -y curl bash
- mkdir -p $NVM_DIR
- curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
- export NVM_DIR=“$HOME/.nvm”
- if [ -s “$NVM_DIR/nvm.sh” ]; then . “$NVM_DIR/nvm.sh”; fi
- nvm install $NODE_VERSION
- nvm use $NODE_VERSION
- nvm alias default $NODE_VERSION
- node -v
- npm -v

install:
stage: install
script:
- npm install
artifacts:
paths:
- node_modules/

build:
stage: build
script:
- npm run build
artifacts:
paths:
- dist/

deploy:
stage: deploy
script:
- eval $(ssh-agent -s)
- echo “$SSH_PRIVATE_KEY_BASE64” | base64 -d > /tmp/ssh_private_key
- chmod 600 /tmp/ssh_private_key
- ssh-add /tmp/ssh_private_key
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- ssh-keyscan -H $VM_IPADDRESS >> ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
- scp package.json $SSH_USER@$VM_IPADDRESS:/tmp/package.json
- scp package-lock.json $SSH_USER@$VM_IPADDRESS:/tmp/package-lock.json
- ssh $SSH_USER@$VM_IPADDRESS "
sudo rm -rf /var/www/html/node_modules &&
sudo cp /tmp/package.json /var/www/html &&
sudo cp /tmp/package-lock.json /var/www/html &&
cd /var/www/html &&
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash &&
source ~/.nvm/nvm.sh &&
nvm install $NODE_VERSION &&
nvm alias default $NODE_VERSION &&
nvm use $NODE_VERSION &&
sudo npm install -g npm@latest --unsafe-perm &&
sudo npm cache clean --force &&
sudo npm install --unsafe-perm &&
sudo npm install -g astro@latest --unsafe-perm &&
sudo npm run build --unsafe-perm"
environment:
name: production
url: http://$VM_IPADDRESS
only:
- main

I tried apt-get, yum, dnf, and chatGPT recommended microdnf, none of them seem to work.

All I’m trying to do it create a runner for an Astro.js project.

Thank you!