Npm: command not found

Problem to solve

npm: command not found

Describe your question in as much detail as possible:
This is the first time I use gitlab and Playwright. I found the “npm: command not found” issue. I already read other users issues that were similar but I couldn’t solve it.

I am working with Angular in WSL using node 18.

Configuration

My dockerfile is:

# This Dockerfile is used to build the services-ui image.
# It starts from the timbru31/java-node:21-jre-iron base image.
FROM timbru31/java-node:21-jre-iron AS build

# Set the working directory inside the container to /app.
WORKDIR /app

# Copy the package.json and package-lock.json files to the /app directory inside the container.
COPY package*.json /app

RUN npm cache clean --force

# Install the Angular CLI globally using npm.
RUN npm install -g @angular/cli

ENV NODE_ENV development

# Install the dependencies specified in the package.json file.
RUN npm ci --ignore-scripts

# Copy all the files and directories from the host machine to the /app directory inside the container.
COPY . .

RUN npm run check

# Build the Angular application.
RUN npm run build

# Add Playwright installation
RUN npm install -g @playwright/test && \
    npx playwright install --with-deps

# Use the nginx:1.23.1-alpine base image
FROM nginx:1.23.1-alpine AS nginx

# Copy the contents of the /app/build/dist directory to the /usr/share/nginx/html directory in the container
COPY --from=build /app/build/dist /usr/share/nginx/html

# Copy custom nginx configuration if needed
COPY default.conf /etc/nginx/nginx.d/default.conf

# Expose port 80 to allow incoming traffic
EXPOSE 80

# Start the nginx server with the "daemon off;" option
CMD ["nginx", "-g", "daemon off;"]

My gitlab-ci.yml is:

before_script:
  - apt-get update && apt-get install -y sudo # Instalar sudo si es necesario
  - sudo chown -R $(whoami) /var/lib/apt/lists/ /var/cache/apt/ # Cambiar el dueño de los directorios
  - sudo apt-get update
  - sudo apt-get install -y nodejs npm

# Clean cache
cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - node_modules/

stages:          # List of stages for jobs, and their order of execution
  - build
  - test
  - merge # This stage create a merge of different reports
  - deploy

build-job:       # This job runs in the build stage, which runs first.
  stage: build
  image: node:18-buster
  script:
    - echo "Compiling the code..."
    - npm ci
    - echo "Compile complete."

shard-test-job-1:   # This job runs in the test stage.
  stage: test    # It only starts when the job in the build stage completes successfully.
  image: node:18-buster
  script:
    - npm ci
    - npx playwright install --with-deps # install browsers and dependencies
    - npx playwright test --reporter blob,junit --shard=1/3
  artifacts:
    when: always
    paths:
      - blob-report/**
      - playwright-tests/results.xml

shard-test-job-2:   # This job runs in the test stage.
  stage: test    # It only starts when the job in the build stage completes successfully.
  image: node:18-buster
  script:
    - npm ci
    - npx playwright install --with-deps # install browsers and dependencies
    - npx playwright test --reporter blob,junit --shard=2/3
  artifacts:
    when: always
    paths:
      - blob-report/**
      - playwright-tests/results.xml

shard-test-job-3:   # This job runs in the test stage.
  image: node:18-buster
  stage: test    # It only starts when the job in the build stage completes successfully.
  script:
    - npm ci
    - npx playwright install --with-deps # install browsers and dependencies
    - npx playwright test --reporter blob,junit --shard=3/3
  artifacts:
    when: always
    paths:
      - blob-report/**
      - playwright-tests/results.xml

merge-test-reports:
  stage: merge
  image: node:18-buster
  script:
    - npm ci
    - npx playwright merge-reports --reporter html ./blob-report
    - npx playwright merge-reports --reporter junit ./blob-report-junit
  artifacts:
    when: always # also run when shards fail
    paths:
      - playwright-report/**
      - playwright-tests/**

lint-test-job:   # This job also runs in the test stage.
  stage: test    # It can run at the same time as unit-test-job (in parallel).
  script:
    - echo "Linting code... This will take about 10 seconds."
    - sleep 10
    - echo "No lint issues found."

deploy-job:
  stage: deploy
  script:
    - echo "Deploying application..."
    - npm run deploy
    - echo "Application successfully deployed."
  environment:
    name: production
    url: https://your-production-url.com # it must be our URL

And when the pipeline run I found many errors, such us:
npm ERR! internal/modules/cjs/loader.js:818

1359npm ERR! throw err;

1360npm ERR! ^

1364npm ERR! - /home/gitlab/builds/iy7pHTNvB/0/services-ui/node_modules/nx/src/daemon/client/client.js

I also tried with:

stages:          # List of stages for jobs, and their order of execution
  - build
  - test
  - merge # This stage create a merge of different reports
  - deploy

build-job:       # This job runs in the build stage, which runs first.
  stage: build
  image: node:16-buster
  script:
    - echo "Compiling the code..."
    - echo "Compile complete."

shard-test-job-1:   # This job runs in the test stage.
  stage: test    # It only starts when the job in the build stage completes successfully.
  image: node:16-buster
  script:
    - npm ci
    - npx playwright install --with-deps # install browsers and dependencies
    - npx playwright test --reporter blob,junit --shard=1/3
  artifacts:
    when: always
    paths:
      - blob-report/**
      - playwright-tests/results.xml
...

and I get the error:
$ npm ci
bash: line 145: npm: command not found
Uploading artifacts for failed job

Versions

I have no idea because it is the first time I use it. How can I know it?

  • Self-managed
  • GitLab.com SaaS
  • Self-hosted Runners

Versions