asp.netnode.jsasp.net-coreasp.net-core-3.1nodeservices

Using ASP.NET Core 3.1 with Node.js?


I have read that NodeServices have been deprecated for ASP.NET Core 3.1. Is it still possible to use Node.js to build my JavaScript files into the dist folder in wwwroot?


Solution

  • According to the NodeServices package, it allows you to "invoke Node.js modules at runtime in ASP.NET Core applications" (emphasis my own). This is reiterated in more detail on the GitHub ReadMe:

    This NuGet package provides a fast and robust way to invoke Node.js code from a .NET application (typically ASP.NET Core web apps). You can use this whenever you want to use Node/NPM-supplied functionality at runtime in ASP.NET.

    That's entirely independent of the ability to e.g. precompile, minimize, or move JavaScript files at build time.

    Build Time Tasks

    You don't—and won't—need NodeServices in order to download npm package dependencies via:

    Likewise, to precompile, minimize, and move client-side dependencies from their source directory to their distribution directory, you can (continue to?) use tools like Gulp.js, Grunt, or WebPack, which are each build systems that operate on top of Node.js.

    Important: The critical distinction here is that you don't need to call these tools at runtime from your .NET application. You are incorporating the output from your Node.js build tools into your .NET application, but you aren't executing Node.js code as part of your .NET application.

    The one exception to this is if you're using NodeService to dynamically perform these build tasks at runtime. For example, if your application is configured with the UseWebpackDevMiddleware() then that will no longer work. In that case, you would need to migrate to a build process that occurs prior to (or during) deployment.

    Webpack

    If you are using UseWebpackDevMiddleware(), then I’d recommend looking into configuring Webpack locally. That should be a pretty seamless transition. You can run it manually via the Webpack CLI, use a Visual Studio extension, or potentially even integrate it into your build process. Personally, I run it manually on my development server, then integrate it into my Azure Pipelines build process.

    Alternatively, if you really want to maintain “just in time” build support for Webpack files, you can look into using the Webpack dev server in conjunction with ASP.NET Core, as discussed in Best way to integrate webpack builds with ASP.NET Core 3.0?.