You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

GitLab Runner Job执行npm安装失败:npm命令未找到求助

Troubleshooting "npm: command not found" in GitLab Runner with ruby:2.5.5 Image

Hey there, let's figure out why your npm command is missing even after setting up Node.js in your before_script. The ruby:2.5.5 base image doesn't come with Node.js pre-installed, so the issue likely lies in how you're installing Node.js or ensuring it's added to your shell's PATH. Here are actionable steps to fix this:

1. Validate Your Node.js Installation Steps

The default apt packages for Node.js in Debian-based images (like ruby:2.5.5) can be outdated or not properly linked. Instead, use the official NodeSource installation script to get a reliable setup:

before_script:
  # Update package lists
  - apt-get update -qq
  # Install dependencies for NodeSource script
  - apt-get install -y -qq curl
  # Add NodeSource repo for a Node.js version compatible with eslint@4.6.1
  - curl -fsSL https://deb.nodesource.com/setup_12.x | bash -
  # Install Node.js and npm
  - apt-get install -y nodejs
  # Verify npm is accessible
  - npm --version
  # Run your global install command
  - npm -g install eslint@v4.6.1 ember-cli yarn@v1.1.0 bower

This script ensures Node.js and npm are installed correctly and added to your system PATH automatically.

2. Debug PATH and npm Location

If you still run into issues, add debug commands to your before_script to pinpoint where things are going wrong:

before_script:
  # ... your existing installation steps ...
  # Check if npm exists on the system
  - which npm
  # Print the current PATH to see if npm's directory is included
  - echo $PATH
  • If which npm returns no output, Node.js/npm didn't install successfully—double-check your installation commands.
  • If it returns a path but that path isn't in $PATH, manually add it:
    - export PATH=$PATH:/path/to/npm/directory
    

3. Use a Pre-Configured Image (Alternative)

If you want to skip manual installation, use a Docker image that includes both Ruby 2.5.5 and Node.js. You can either:

  • Build your own custom image with both dependencies, or
  • Use a community-maintained image. For example:
    # Use an image with Ruby 2.5 and Node.js pre-installed
    image: jitesoft/ruby-node:2.5-node12
    before_script:
      # Verify both Ruby and Node.js are available
      - ruby --version
      - npm --version
      - npm -g install eslint@v4.6.1 ember-cli yarn@v1.1.0 bower
    

4. Fix Global npm Permissions (Optional)

Sometimes global npm installs fail due to permission issues (even if npm is found). To avoid this, configure npm to use a user-specific global directory:

before_script:
  # ... install Node.js ...
  # Create a custom npm global directory
  - mkdir -p ~/.npm-global
  # Configure npm to use this directory
  - npm config set prefix '~/.npm-global'
  # Add the directory to PATH
  - export PATH=~/.npm-global/bin:$PATH
  # Run your global install
  - npm -g install eslint@v4.6.1 ember-cli yarn@v1.1.0 bower

Start with the NodeSource installation method—it's the most reliable for getting a working Node.js/npm setup in Debian-based images. The debug commands will help you quickly spot if PATH is the issue or if installation is failing silently.

内容的提问来源于stack exchange,提问作者Daniel

火山引擎 最新活动