简体   繁体   English

构建 ASP.NET Core 项目时如何自动安装 npm 模块

[英]How to auto install npm modules when I build ASP.NET Core project

I am building a project with ASP.NET Core Web API as back-end, with React SPA as front-end, the folder structure looks like this:我正在构建一个以 ASP.NET Core Web API 作为后端,以 React SPA 作为前端的项目,文件夹结构如下所示:

WebApiProject
│
├── ClientApp/
│   ├── public/
│   ├── src/
│   └── package.json
│
├── Controller/
├── Models/
├── appsettings.json
├── Program.cs
└── Startup.cs

When I add new npm modules to the React project (which will update package.json), and push to our team Git repo, other team members have to manually do the npm install in the ClientApp directory after they pull new commits, otherwise build process will fail due to missing dependencies.当我将新的 npm 模块添加到 React 项目(将更新 package.json),并推送到我们团队的 Git repo 时,其他团队成员必须在他们拉新提交后手动在ClientApp目录中进行npm install ,否则构建过程将由于缺少依赖项而失败。

For the front-end developers it's quite normal, yet for some of the developers who solely focus on the back-end development, it's quite troublesome, that they have to do some extra process to be able to build the project (they normally just ctrl + F5 and it's good to go).对于前端开发者来说,这很正常,但对于一些只专注于后端开发的开发者来说,就相当麻烦,他们必须做一些额外的过程才能构建项目(他们通常只是ctrl + F5就可以了)。


I've tried the following:我尝试了以下方法:

  • Add the nodejs directory to Tools -> Options -> External Web Tools , I thought this will acknowledge IDE to do the work for me, yet it seems not.将 nodejs 目录添加到Tools -> Options -> External Web Tools ,我认为这会承认 IDE 为我做这项工作,但似乎不是。
  • The original .csproj file provides a method like this:原始.csproj文件提供了这样的方法:

<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
  <!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
  <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
  <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build" />

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

It's seems like some sort of auto script for ASP.NET project when it comes to publish, yet I don't quite know how to tweak it so that it do the work during development mode build-time.在发布时,这似乎是 ASP.NET 项目的某种自动脚本,但我不太知道如何调整它以使其在开发模式构建时完成工作。


Is there a way to make the process of npm install comes alone with the ASP.NET IDE build process?有没有办法让npm install的过程独立于 ASP.NET IDE 构建过程? In other word, I'm looking for a way to make npm modules auto installed when building the ASP.NET project.换句话说,我正在寻找一种在构建 ASP.NET 项目时自动安装 npm 模块的方法。

Your config is correct but that is only work for publish your project add this to your csproj file您的配置是正确的,但这仅适用于发布您的项目将其添加到您的 csproj 文件中

 <Target Name="DebugEnsureNodeEnv" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('$(SpaRoot)node_modules') ">
    <!-- Ensure Node.js is installed -->
    <Exec Command="node --version" ContinueOnError="true">
      <Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
    </Exec>
    <Error Condition="'$(ErrorCode)' != '0'" Text="Node.js is required to build and run this project. To continue, please install Node.js from https://nodejs.org/, and then restart your command prompt or IDE." />
    <Message Importance="high" Text="Restoring dependencies using 'npm'. This may take several minutes..." />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
  </Target>

This will assure your project run npm install before you start ASP.Net Core project这将确保您的项目在启动 ASP.Net Core 项目之前运行npm install

I Used this... I Just change a little bit the WorkingDirectory adding ClientApp我用过这个......我只是改变了一点点工作目录添加客户端应用程序

<Target Name="DebugEnsureNodeEnv" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('$(SpaRoot)ClientApp\node_modules') ">
        <Exec Command="node --version" ContinueOnError="true">
            <Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
        </Exec>
        <Error Condition="'$(ErrorCode)' != '0'" Text="Node.js is required to build and run this project. To continue, please install Node.js from https://nodejs.org/, and then restart your command prompt or IDE." />
        
        <Message Importance="high" Text="Restoring dependencies using 'npm'. This may take several minutes..." />
        
        <Exec WorkingDirectory="$(SpaRoot)ClientApp" Command="npm install" />
    </Target>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 ASP.NET Core 中使用 npm - How to use npm with ASP.NET Core 在ASP.NET核心中使用时,如何从另一个项目访问EF核心的DbContext? - How to I access the DbContext of EF core from another project when used in ASP.NET core? 我打算何时建立项目的ASP.NET - ASP.NET when am I meant to build my project 如何在 ASP.NET Core 中排除项目依赖项? - How do I exclude project dependencies in ASP.NET Core? 如何在ASP.NET项目中安装CamanJS - How do I install CamanJS in a ASP.NET project 当ASP.NET Core 2.2项目遇到特定异常时,如何重定向用户? - How can I redirect a user when a specific exception is cought with ASP.NET Core 2.2 project? 如何使用Visual Studio代码将实体框架核心安装到asp.net核心3项目 - How to install entity framework core to asp.net core 3 project using visual studio code 如何使用 powershell 为构建服务器构建、发布 ASP.NET 核心项目? - How to build, publish ASP.NET core project using powershell for build server? 未使用 docker 命令构建新的 Asp.Net 核心项目 - New Asp.Net core project not getting build with docker command 发布时从另一个ASP.NET Core 2项目引用ASP.NET Core 2项目失败 - Referencing ASP.NET Core 2 project from another ASP.NET Core 2 project fails when publishing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM