Cannot find module 'aspnet-webpack' when using 'dotnet publish' in .Net Core 2.0 & Angular Cannot find module 'aspnet-webpack' when using 'dotnet publish' in .Net Core 2.0 & Angular angular angular

Cannot find module 'aspnet-webpack' when using 'dotnet publish' in .Net Core 2.0 & Angular


After you publish your .Net Core 2.0 & Angular project, and before you run it, you need to ensure that the environment variable ASPNETCORE_ENVIRONMENT isn't set to Development. The published project doesn't include support for the WebpackDevMiddleware or HotModuleReplacement. But it will attempt to use them if the environment is set to Development.

HotModuleReplacement automatically updates Webpack-built resources (such as JavaScript, CSS, or images) in your web browser whenever source files are changed. This is obviously something that you don't want in production.

If ASPNETCORE_ENVIRONMENT is set to "Development", you can change the setting with:

setx ASPNETCORE_ENVIRONMENT "Production"

You will need to close the current command window and open another to see the change.

You can also comment out the following code in startup.cs to accomplish the same result:

#if DEBUGapp.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions{    HotModuleReplacement = true});#endif


Make sure you have fully installed all of these dependencies:

npm install webpack-hot-middleware --save-devnpm install webpack-dev-middleware --save-devnpm install aspnet-webpack --save-dev


In my case the cause was that the SPA (Vue in my case) is in the ClientApp folder and app.UseWebpackDevMiddleware expects it to be in the project root.

Setting the ProjectPath option solved this for me.

app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions  {    HotModuleReplacement = true,    ConfigFile = Path.Combine(env.ContentRootPath, @"ClientApp\node_modules\@vue\cli-service\webpack.config.js"),    ProjectPath = Path.Combine(env.ContentRootPath, @"ClientApp")  });