GitLab CI/CD - npm not found

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.

OK, so looking through this, it seems to me that your 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:

    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:

    stage: deploy
        - 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

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