[英]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:我尝试了以下方法:
Tools -> Options -> External Web Tools
, I thought this will acknowledge IDE to do the work for me, yet it seems not.Tools -> Options -> External Web Tools
,我认为这会承认 IDE 为我做这项工作,但似乎不是。.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.