'Nest build fails in pipeline: Cannot find modules which can be found when building locally

In my NestJS project, building and running locally works, but when building the same code in my Azure pipeline, it has recently started failing due to some modules not being found when the command npm run build starts. When I run the command locally, there are no issues and it builds correctly.

There are many modules that do get found correctly (or not giving an error), so why don't these? You can find an image of my file layout in this image on Imgur with the relevant files visible. All the classes are declared as export class ... {} and most of them are also imported in the AppModule, where they don't give an error.

The pipeline layout (relevant parts):

trigger:
  - master
  - dev

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: NodeTool@0
  displayName: 'Install Node 12'
  inputs:
    versionSpec: 12.0.0

- script: |
    npm install -g typescript
    npm install
    npm test
  displayName: 'run tests'

- script: |
    npm run build
  displayName: 'build dist folder'

The pipeline logs: (relevant parts)

Generating script.
Script contents:
npm run build
========================== Starting Command Output ===========================

> [email protected] prebuild /home/vsts/work/1/s
> rimraf dist


> [email protected] build /home/vsts/work/1/s
> nest build

10 import { MailerService } from "../../Mailer/mailer.service";
                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/app.module.ts:13:30 - error TS2307: Cannot find module './Mailer/mailer.module'.

13 import { MailerModule } from './Mailer/mailer.module';
                                ~~~~~~~~~~~~~~~~~~~~~~~~
src/app.module.ts:14:34 - error TS2307: Cannot find module './Mailer/mailer.controller'.

14 import { MailerController } from './Mailer/mailer.controller';
                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/app.module.ts:15:33 - error TS2307: Cannot find module './Teams/teams.controller'.

15 import { TeamsController } from './Teams/teams.controller';
                                   ~~~~~~~~~~~~~~~~~~~~~~~~~~
src/app.module.ts:16:37 - error TS2307: Cannot find module './Chatlayer/chatlayer.controller'.

16 import { ChatlayerController } from './Chatlayer/chatlayer.controller';
                                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/mailer/mailer.module.ts:4:33 - error TS2307: Cannot find module '../Mailer/mailer.provider'.

4 import { mailerProviders } from "../Mailer/mailer.provider";
                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~

Found 9 error(s).

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] build: `nest build`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/vsts/.npm/_logs/2020-06-03T18_00_47_946Z-debug.log

##[error]Bash exited with code '1'.
Finishing: build dist folder


Solution 1:[1]

It worked by changing the build agent to macOs. It must indeed have been the case sensitivity which was wrong somewhere.

Solution 2:[2]

I had a similar problem with bitbucket pipeline. It was because i had written npm install and npm run build under different steps and hence it was not able to find the module required for the next step.

Solution 3:[3]

I had a similar problem when implementing testing into an azure pipeline. In my case it was a module called karma.config.ci.js. Had to add this file in my root directory to resolve the problem.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jasper Rosiers
Solution 2 Tal
Solution 3 MikhailRatner