发布ASP.NET核心应用程序:命令“npm install”退出,代码为9009

iyfamqjs  于 2023-02-01  发布在  .NET
关注(0)|答案(2)|浏览(169)

我有一个带有React前端的ASP.NET核心解决方案。我现在遇到了一个问题,我无法使用Visual Studio内的正常发布窗口发布代码。我发布到Azure Web应用程序内的Web应用程序,它对我所有其他以相同方式构建的解决方案都能完美工作。
代码在本地工作得很完美,我可以在本地运行npm install,没有任何问题。
这个错误显然来自发布时找不到的某个文件。在我的项目文件中,我一直在调试,发现这是一个挑战:

<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
    <!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
    <Exec Command="npm install" />
    <Exec Command="node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.js --env.prod" />
    <Exec Command="node node_modules/webpack/bin/webpack.js --env.prod" />

    <!-- Include the newly-built files in the publish output -->
    <ItemGroup>
      <DistFiles Include="wwwroot\dist\**" />
      <ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
        <RelativePath>%(DistFiles.Identity)</RelativePath>
        <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
      </ResolvedFileToPublish>
    </ItemGroup>
  </Target>

Visual Studio中的错误

Severity    Code    Description Project File    Line    Suppression State
Error       The command "npm install" exited with code 9009.    Likvido.CreditRisk  C:\Users\MYNAME\Documents\Github\Likvido.CreditRisk\Likvido.CreditRisk\Likvido.CreditRisk\Likvido.CreditRisk.csproj 75

如果我注解掉前三行(npm install和两个webpack),我就可以发布解决方案,但显然不能使用JavaScript。
你知道如何解决这个问题吗?至少,如何更好地调试它?

Visual Studio中的可视错误

GUI中引用的日志文件:

09/04/2018 11.07.03
System.AggregateException: One or more errors occurred. ---> System.Exception: Publish failed due to build errors. Check the error list for more details.
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at Microsoft.VisualStudio.Web.Publish.PublishService.VsWebProjectPublish.<>c__DisplayClass40_0.<PublishAsync>b__2()
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.ApplicationCapabilities.Publish.ViewModel.ProfileSelectorViewModel.<RunPublishTaskAsync>d__88.MoveNext()
---> (Inner Exception #0) System.Exception: Publish failed due to build errors. Check the error list for more details.<---

===================
zwghvu4y

zwghvu4y1#

这很尴尬,但对未来的谷歌人来说可能有用。
根本问题很简单:这是一台没有安装Node.js(或NPM)的新计算机。这使得错误消息非常有用:页面没有找到!
解决办法很简单:安装Node.js,确定它在你的路径中,然后重新启动你的电脑。然后你就可以解决这个问题了。

gorkyyrv

gorkyyrv2#

如果您从Visual Studio安装程序安装node.js工具,默认情况下npm不在您的路径中,因此类似下面的内容将无法工作:

<Target Name="RunNPMInstall" BeforeTargets="PreBuildEvent">
    <Exec Command="npm install" />
</Target>

其他人建议编辑您的环境以将npm的路径添加到其中,但这很烦人,如果您更改Visual Studio的主要版本,它将会中断。
相反,您可以执行类似以下的操作,使Exec命令引用Visual Studio安装程序安装node.js工具的公共相对子路径:

<Target Name="RunNPMInstall" BeforeTargets="PreBuildEvent">
    <PropertyGroup>
        <npmPath>"$(VsInstallRoot)\Msbuild\Microsoft\VisualStudio\NodeJs\npm.cmd"</npmPath>
    </PropertyGroup>
    <Exec Command="echo Running '$(npmPath)'" />
    <Exec Command="$(npmPath) install" ConsoleToMsBuild="true" />
    <Exec Command="echo Completed running '$(npmPath)'" />
</Target>

这将从正确的路径执行npm:

1>Running '"C:\Program Files\Microsoft Visual Studio\2022\Professional\Msbuild\Microsoft\VisualStudio\NodeJs\npm.cmd"'
1>
1>up to date, audited 986 packages in 2s
1>
1>89 packages are looking for funding
1>  run `npm fund` for details
1>
1>30 vulnerabilities (3 low, 6 moderate, 14 high, 7 critical)
1>
1>To address issues that do not require attention, run:
1>  npm audit fix
1>
1>To address all issues possible (including breaking changes), run:
1>  npm audit fix --force
1>
1>Some issues need review, and may require choosing
1>a different dependency.
1>
1>Run `npm audit` for details.
1>Completed running '"C:\Program Files\Microsoft Visual Studio\2022\Professional\Msbuild\Microsoft\VisualStudio\NodeJs\npm.cmd"'

相关问题