【问题标题】:Angular build cannot find module with relative path (on azure dev ops)Angular 构建找不到具有相对路径的模块(在 azure devops 上)
【发布时间】:2019-06-26 13:29:56
【问题描述】:

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

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

ng new minimal-app

它可以在本地构建,也可以在 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';
  }

}

User.model.ts 文件包含

export class UserModel {
  id: number;
  name: string;
}

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

export class ActionModel {
  id: number;
  datetime: string;
}

并更改用户模型

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

export class UserModel {
  id: number;
  name: string;
  actions: ActionModel[];
}

这在本地工作,但在 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'

我没有更改任何 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'

【问题讨论】:

  • 也许尝试将. 添加到./Action.Model (../Action.Model)
  • @ShaykiAbramczyk 遗憾的是,这无济于事:(尽管这些模型彼此位于同一个文件夹中,所以我想这是一个远射,但我对此感到困惑
  • 您可以在尝试构建应用程序时修复它通过使用 baseHref 和 deployUrl

标签: angular azure-devops


【解决方案1】:

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

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

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

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

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

export class UserModel {
  id: number;
  name: string;
  actions: ActionModel[];
}

【讨论】:

  • 解决了!谢谢,很好发现:)
猜你喜欢
  • 2022-11-07
  • 1970-01-01
  • 2021-01-09
  • 1970-01-01
  • 2017-01-05
  • 1970-01-01
  • 1970-01-01
  • 2014-06-26
相关资源
最近更新 更多