Deploying NestJS to Azure DevOps: Error: Cannot find module '../commands'

Deploying NestJS to Azure DevOps: Error: Cannot find module '../commands'

我有一个非常基本的 "Hello World" NestJS 应用程序,我正在尝试将其部署到 Azure DevOps 中的 Azure Web 应用程序实例。

我已经使用 YAML 文件设置了构建管道,该文件输出带有 distnode_modules 目录的构建工件。

然后,在我的发布管道中,我设置了一个持续部署来下载该工件并进行部署。发布管道包含一个部署 Azure 应用服务的步骤。此外,在此步骤之后,我输入了一些 Post-部署操作,它们是 npm installnpm updatenpm run start:prod 以启动 NestJS 服务器。

然而,当 运行 管道时,该步骤花费了过多的时间,最终出错:

当我访问我设置的网络应用程序实例 (https://<project-name>.azurewebsites.net/) 时,我看到:

所以,我点击 "Diagnostic Resources" 试图找出我的发布失败的原因并最终发现了这个错误:

2020-05-29T20:01:30.864090341Z   _____                               
2020-05-29T20:01:30.864143041Z   /  _  \ __________ _________   ____  
2020-05-29T20:01:30.864149941Z  /  /_\  \___   /  |  \_  __ \_/ __ \ 
2020-05-29T20:01:30.864153741Z /    |    \/    /|  |  /|  | \/\  ___/ 
2020-05-29T20:01:30.864157341Z \____|__  /_____ \____/ |__|    \___  >
2020-05-29T20:01:30.864161141Z         \/      \/                  \/ 
2020-05-29T20:01:30.864164541Z A P P   S E R V I C E   O N   L I N U X
2020-05-29T20:01:30.864167941Z 
2020-05-29T20:01:30.864171141Z Documentation: http://aka.ms/webapp-linux
2020-05-29T20:01:30.864174441Z NodeJS quickstart: https://aka.ms/node-qs
2020-05-29T20:01:30.864177741Z NodeJS Version : v12.13.0
2020-05-29T20:01:30.864181041Z Note: Any data outside '/home' is not persisted
2020-05-29T20:01:30.864184441Z 
2020-05-29T20:01:30.951441760Z Oryx Version: 0.2.20191105.2, Commit: 67e159d71419415435cb5d10c05a0f0758ee8809, ReleaseTagName: 20191105.2
2020-05-29T20:01:30.951840160Z Cound not find build manifest file at '/home/site/wwwroot/oryx-manifest.toml'
2020-05-29T20:01:30.951915661Z Could not find operation ID in manifest. Generating an operation id...
2020-05-29T20:01:30.952061661Z Build Operation ID: dfd18989-3b7b-4e2e-a64e-cf7310361e99
2020-05-29T20:01:32.750738663Z Writing output script to '/opt/startup/startup.sh'
2020-05-29T20:01:33.355033998Z Running #!/bin/sh
2020-05-29T20:01:33.359017599Z 
2020-05-29T20:01:33.359033399Z # Enter the source directory to make sure the script runs where the user expects
2020-05-29T20:01:33.359644199Z cd "/home/site/wwwroot"
2020-05-29T20:01:33.359658399Z 
2020-05-29T20:01:33.359664799Z export NODE_PATH=$(npm root --quiet -g):$NODE_PATH
2020-05-29T20:01:33.360538899Z if [ -z "$PORT" ]; then
2020-05-29T20:01:33.360552699Z         export PORT=8080
2020-05-29T20:01:33.360557899Z fi
2020-05-29T20:01:33.360561499Z 
2020-05-29T20:01:33.361582699Z npm start
2020-05-29T20:01:35.698639821Z 
2020-05-29T20:01:35.698668521Z > <project-name>@0.0.1 start /home/site/wwwroot
2020-05-29T20:01:35.698675121Z > nest start
2020-05-29T20:01:35.698679221Z 
2020-05-29T20:01:35.930729770Z internal/modules/cjs/loader.js:797
2020-05-29T20:01:35.930766970Z     throw err;
2020-05-29T20:01:35.930772870Z     ^
2020-05-29T20:01:35.930776770Z 
2020-05-29T20:01:35.930780670Z Error: Cannot find module '../commands'
2020-05-29T20:01:35.930784670Z Require stack:
2020-05-29T20:01:35.930788470Z - /home/site/wwwroot/node_modules/.bin/nest
2020-05-29T20:01:35.930792370Z     at Function.Module._resolveFilename (internal/modules/cjs/loader.js:794:15)
2020-05-29T20:01:35.930803370Z     at Function.Module._load (internal/modules/cjs/loader.js:687:27)
2020-05-29T20:01:35.930807770Z     at Module.require (internal/modules/cjs/loader.js:849:19)
2020-05-29T20:01:35.930811570Z     at require (internal/modules/cjs/helpers.js:74:18)
2020-05-29T20:01:35.930815370Z     at Object. (/home/site/wwwroot/node_modules/.bin/nest:5:20)
2020-05-29T20:01:35.930819770Z     at Module._compile (internal/modules/cjs/loader.js:956:30)
2020-05-29T20:01:35.930823570Z     at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
2020-05-29T20:01:35.930827470Z     at Module.load (internal/modules/cjs/loader.js:812:32)
2020-05-29T20:01:35.930831270Z     at Function.Module._load (internal/modules/cjs/loader.js:724:14)
2020-05-29T20:01:35.930835070Z     at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10) {
2020-05-29T20:01:35.930838970Z   code: 'MODULE_NOT_FOUND',
2020-05-29T20:01:35.930842770Z   requireStack: [ '/home/site/wwwroot/node_modules/.bin/nest' ]
2020-05-29T20:01:35.930846670Z }
2020-05-29T20:01:35.970252977Z npm ERR! code ELIFECYCLE
2020-05-29T20:01:35.979824678Z npm ERR! errno 1
2020-05-29T20:01:35.981324478Z npm ERR! <project-name>@0.0.1 start: `nest start`
2020-05-29T20:01:35.982002178Z npm ERR! Exit status 1
2020-05-29T20:01:35.982641778Z npm ERR! 
2020-05-29T20:01:35.991823280Z npm ERR! Failed at the <project-name>@0.0.1 start script.
2020-05-29T20:01:35.991838780Z npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-05-29T20:01:36.118651500Z 
2020-05-29T20:01:36.119439400Z npm ERR! A complete log of this run can be found in:
2020-05-29T20:01:36.128548002Z npm ERR!     /root/.npm/_logs/2020-05-29T20_01_35_990Z-debug.log

此时我无法弄清楚哪里出了问题。任何帮助将不胜感激。

更有可能的是,您使用的 npm start 默认映射到 nest start。你会 运行 遇到的问题是 @nestjs/clinest 命令的来源)默认是 devDependencies 的一部分,所以除非你已经移动它,Azure 可能正在清理 devDeps 并保持在产品部门。您应该使用 node dist/main 来启动您的应用程序,而不是在不增加依赖项的情况下消除此问题。否则,将 @nestjs/cli 添加到 dependencies 应该可以解决它。