Docker command `npm run` behaves different in GitLab and in local machine

I built the follow Docker container

FROM node:20-alpine as ember
WORKDIR /usr/app
COPY package*.json /usr/app/
RUN apk fix && \
    apk --no-cache --update add git && \
    npm clean-install

and uploaded to a registery.

npm run built works as expected from docker compose up with compose.yaml as

services:
  frontend:
    image: my-docker-image
    volumes:
      - type: bind
        source: .
        target: /usr/app
    command: npm run build

When I try to run npm run build from GitLab CI, I get the error sh: ember: not found. The GitLab CI task/job is

npm-run:
  image: my-docker-image
  script:
    - npm run build

Why npm is not working in GitLab CI?

GitLab Environment Information

Self-hosted GitLab Community Edition v16.3.4

Local Environment Information

Ubuntu 22.04 LTS

Support files

Solution (Edited)

GitLab CI overwrite Docker’s WORKDIR to /builds/username/repository. Because of this, the current directory (/builds/username/repository) did not have the node_modules as it was installed in /usr/app/ and npm run built fails.

In the local machine, the development node_modules also overwrite the node_modules in the Docker container. Because of this, npm run built runs as expected.

Change my GitLab CI configuration to

npm-run:
  image: my-docker-image
  script:
    - ln -s ../../../usr/app/node_modules node_modules
    - npm run build

resolved the problem.

Additional note: Change my GitLab CI configuration to

npm-run:
  image: my-docker-image
  script:
    - npm run --prefix ../../../usr/app build

did not resolved the problem because --prefix ../../../usr/app is equivalent to cd ../../../usr/app and npm couldn’t find my project files.

1 Like