首页 > 解决方案 > 使用 Docker 映像作为 azure 管道构建代理时出现“找不到模块”

问题描述

我正在尝试使用我创建的 Docker 映像作为 Azure 管道中的构建代理。我正在使用 Azure DevOps Server 2019。

我的 Docker 映像是使用 nodejs 构建的,并且安装了我的 npm 依赖项。我使用 Docker 映像作为 Angular 应用程序的构建代理。

这是我使用的 Dockerfile:

FROM mcr.microsoft.com/windows:1809-amd64

#Create app directory
WORKDIR /

# Install Chocolatey
RUN powershell -ExecutionPolicy unrestricted -Command iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

#install node and npm
RUN choco install -y nodejs-lts --version 14.15.0
#install angular-cli
RUN npm install -g @angular/cli
RUN refreshenv

#Install app dependencies
COPY ./client/package*.json ./

RUN npm ci

CMD powershell

这是我正在使用的管道 YAML:

trigger:
  none
resources:
  containers:
  - container: angulardocker
    image: angular-dev:1.0
    endpoint: Docker Hub

stages:
  - stage: Client
    jobs:
      - job: Build
        pool:
          name: Default
        container: angulardocker
        steps:
          - task: Npm@1
            displayName: 'Client Build'
            inputs:
              command: custom
              customCommand: run build -- --prod
              workingDir: client
          - task: CopyFiles@2
            displayName: 'Copy Client Build to Staging Directory'
            inputs:
              contents: '**'
              SourceFolder: $(Build.SourcesDirectory)/client/dist
              targetFolder: $(Build.ArtifactStagingDirectory)/client
          - task: PublishBuildArtifacts@1
            displayName: 'Publish Build Artifact - Client'
            inputs:
              pathToPublish: '$(Build.ArtifactStagingDirectory)/client'
              artifactName: 'Client'

当管道运行时,从 Docker Hub 中提取 Docker 映像并成功初始化为容器,但在执行“npm run build --prod”构建步骤时会引发错误。这是出现的错误消息:

; environment configs
cache = "C:\\__w\\6\\.npm"
userconfig = "C:\\__w\\6\\npm\\346.npmrc"

; builtin config undefined
prefix = "C:\\Users\\ContainerAdministrator\\AppData\\Roaming\\npm"

; node bin location = C:\Program Files\nodejs\node.exe
; cwd = C:\__w\6\s\client
; HOME = C:\Users\ContainerAdministrator
; "npm config ls -l" to show all defaults.

[command]C:\Windows\system32\cmd.exe /D /S /C ""C:\Program Files\nodejs\npm.cmd" run build -- --prod"
internal/modules/cjs/loader.js:883
  throw err;
  ^

Error: Cannot find module 'C:\__w\6\s\client\node_modules\@angular\cli\bin\ng'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
    at Function.Module._load (internal/modules/cjs/loader.js:725:27)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)

    at internal/main/run_main_module.js:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

管道说它找不到 angular-cli 节点模块。我自己运行了 Docker 映像,并且 node_modules 文件夹确实存在。我还尝试将构建步骤更改为 ' ng build --prod'using powershell 并收到此错误:An unhandled exception occurred: Cannot locate the 'node_modules' directory.

如何让管道找到 node_modules 文件夹?

标签: docker

解决方案


您可以尝试--unsafe-perm在 Dockerfile 中使用 npm 的开关,看看它是否可以工作。

RUN npm -g install nodegit --unsafe-perm

要查看更多详细信息,您可以参考以下文章:


推荐阅读