I'm using npm to manage the jQuery, Bootstrap, Font Awesome and similar client libraries I need for my ASP.NET Core application.
The approach that worked for me started by adding a package.json file to the project, that looks like this:
{
"version": "1.0.0",
"name": "myapp",
"private": true,
"devDependencies": {
},
"dependencies": {
"bootstrap": "^3.3.6",
"font-awesome": "^4.6.1",
"jquery": "^2.2.3"
}
}
npm restores these packages into the node_modules folder which is on the same level as wwwroot in the project directory:
As ASP.NET Core serves the static files from the wwwroot folder, and node_modules is not there, I had to make a couple of changes to make this work, the first one: adding app.UseFileServer right before app.UseStaticFiles in my Startup.cs file:
app.UseFileServer(new FileServerOptions()
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), @"node_modules")),
RequestPath = new PathString("/node_modules"),
EnableDirectoryBrowsing = true
});
app.UseStaticFiles();
and the second one, including node_modules in my publishOptions in the project.json file:
"publishOptions": {
"include": [
"web.config",
"wwwroot",
"Views",
"node_modules"
]
},
This works in my development environment and it also works when I deploy it to my Azure App Service instance, the jquery, bootstrap and font-awesome static files get served well, but I'm not sure about this implementation.
What is the right approach for doing this?
This solution came after collecting lots of bits of info from several sources and trying some that didn't work, and it seems a bit odd having to serve these files from outside wwwroot.
Any advice will be greatly appreciated.
Install the Bundler and Minifier into Visual Studio Extensions
Then you create a
bundleconfig.json
and enter the following like :So the bundler and minifier (gulp based) has access to the source files (which should be excluded from Visual Studio and also excluded from GIT) and puts them into the wwwroot as specified
only side effect every time you save it will run this (or you can set it or run it manually)
I've found a better way how to manage JS packages in my project with NPM Gulp/Grunt task runners. I don't like the idea to have a NPM with another layer of javascript library to handle the "automation", and my number one requirement is to simple run the npm update without any other worries about to if I need to run gulp stuff, if it successfully copied everything and vice versa.
The NPM way:
The python way:
npm
for managing client-side libraries is a good choice (as opposed to Bower or NuGet), you're thinking in the right direction :)FileServer
, havingStaticFiles
should be suffice for serving static files (.js, images etc.)wwwroot
topublic
, otherwise the folder structure in Azure Web Apps will be confusing (D:\Home\site\wwwroot\wwwroot
vsD:\Home\site\wwwroot\public
)node_modules
to a web hosting server). Seetools/deploy.js
as an example.Visit ASP.NET Core Starter Kit on GitHub (disclaimer: I'm the author)
Shawn Wildermuth has a nice guide here: https://wildermuth.com/2017/11/19/ASP-NET-Core-2-0-and-the-End-of-Bower
The article links to the gulpfile on GitHub where he's implemented the strategy in the article. You could just copy and paste most of the gulpfile contents into yours, but be sure to add the appropriate packages in package.json under devDependencies: gulp gulp-uglify gulp-concat rimraf merge-stream
Much simpler approach is to use OdeToCode.UseNodeModules Nuget package. I just tested it with .Net Core 3.0. All you need to do is add the package to the solution and reference it in the Configure method of the Startup class:
I learned about it from the excellent Building a Web App with ASP.NET Core, MVC, Entity Framework Core, Bootstrap, and Angular Pluralsight course by Shawn Wildermuth.
Instead of trying to serve the node modules folder, you can also use Gulp to copy what you need to wwwroot.
https://docs.asp.net/en/latest/client-side/using-gulp.html
This might help too
Visual Studio 2015 ASP.NET 5, Gulp task not copying files from node_modules