Angular 构建找不到具有相对路径的模块(在 azure dev ops 上)

Cod*_*key 4 azure-devops angular

在 Azure Dev Ops Pipelines 上构建中型 Angular 7 项目时遇到问题。所以我创建了一个最小的项目来重现这个问题。

使用 Angular CLI (7.2.4) 我创建了一个新项目

ng new minimal-app
Run Code Online (Sandbox Code Playgroud)

它在本地和 Azure 上构建都很好。我编辑了 app.component.ts 以使用我的 UserModel,第 2、11 和 14 行

import {Component} from '@angular/core';
import {UserModel} from '../Models/User.model';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'minimal-app';
  user = new UserModel();

  constructor() {
    this.user.name = 'Jo';
  }

}
Run Code Online (Sandbox Code Playgroud)

User.model.ts 文件包含

export class UserModel {
  id: number;
  name: string;
}
Run Code Online (Sandbox Code Playgroud)

这也可以在本地和 Azure 上编译得很好。尝试添加嵌套模型时出现问题,请参阅 Action.model.ts

export class ActionModel {
  id: number;
  datetime: string;
}
Run Code Online (Sandbox Code Playgroud)

并更改 UserModel

import {ActionModel} from './Action.Model';

export class UserModel {
  id: number;
  name: string;
  actions: ActionModel[];
}
Run Code Online (Sandbox Code Playgroud)

这在本地有效,但在 Azure Dev Ops 上我得到

ERROR in src/Models/User.model.ts(1,27): error TS2307: Cannot find module './Action.Model'.

##[error]Bash exited with code '1'
Run Code Online (Sandbox Code Playgroud)

我没有更改任何 Angular 配置文件,它们都是新项目的默认设置。天蓝色管道 yml 是

# Node.js with Angular
# Build a Node.js project that uses Angular.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/javascript

trigger:
- master

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: NodeTool@0
  inputs:
    versionSpec: '10.x'
  displayName: 'Install Node.js'

- script: |
    npm install -g @angular/cli
    npm install
    ng build --prod
  displayName: 'npm install and build'
Run Code Online (Sandbox Code Playgroud)

Mer*_*SFT 8

根据您的代码,我注意到您正在使用区分大小写的Ubuntu代理。

而且,您配置的模块是“ Action.model ”,但是在导入时,您使用的名称是“ Action.Model ”。对于 ubuntu 和 linux,由于区分大小写,无法将其识别为相同。

我认为您可以使用不区分大小写的 Windows 在本地执行这些脚本。这就是它在本地工作的原因。

因此,为了解决,您需要将脚本更改为:

import {ActionModel} from './Action.model';

export class UserModel {
  id: number;
  name: string;
  actions: ActionModel[];
}
Run Code Online (Sandbox Code Playgroud)