【问题标题】:Getting 'module not found' when using Docker image as azure pipeline build agent使用 Docker 映像作为 azure 管道构建代理时出现“找不到模块”
【发布时间】:2021-03-19 21:45:19
【问题描述】:

我正在尝试使用我创建的 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 映像会从 Docker Hub 中提取并成功初始化为容器,但在执行“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 文件夹确实存在。我还尝试使用 powershell 将构建步骤更改为 'ng build --prod' 并收到此错误:An unhandled exception occurred: Cannot locate the 'node_modules' directory.

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

【问题讨论】:

    标签: docker


    【解决方案1】:

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

    RUN npm -g install nodegit --unsafe-perm
    

    查看更多详情,可以参考以下文章:

    【讨论】:

    • 我在安装 angular-cli 之前添加了安装 nodegit 步骤。仍然出现错误:Cannot locate the 'node_modules' directory.
    • 嗨@mdailey77,如何将COPY ./client/package*.json ./ 移动到WORKDIR / 之后。例如:WORKDIR / <br> COPY ./client/package*.json ./
    • 我将COPY ./client/package*.json ./ 移动到WORKDIR / 之后,仍然出现同样的错误。
    猜你喜欢
    • 1970-01-01
    • 2021-10-28
    • 2021-09-02
    • 1970-01-01
    • 2022-07-14
    • 2020-03-29
    • 2015-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多