Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 847
Michael Thompson Avatar asked Apr 02 '20 15:04

Michael Thompson


People also ask

What is NET Core 3.1 used for?

NET Core 3.1 is also known for Blazor, the ASP.NET Core technology that puts WebAssembly to use as a browser compilation target for higher-order programming languages so they can be used in Web development instead of JavaScript.

Should I use .NET Core or NodeJS?

NET Core is definitely a winner in this category. The security and reliability the platform provides make it a great option to create robust software with C# language. Node. js is more reliable for complex enterprise software developed with TypeScript than on its own.

Is NodeJS faster than .NET Core?

NET Core has an easier time working with CPU-intensive tasks and rendering static pages since the in-built IIS server kernel caching makes this process very straightforward. Therefore, . NET core vs node. js performance offers different advantages for various projects.

Is .NET Core 3.1 stable?

The extra two months (after . NET Core 3.0) allowed us to select and implement the right set of improvements over what was already a very stable base. . NET Core 3.1 is now ready to be used wherever your imagination or business need takes it.


1 Answers

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:

  • The command prompt on your local workstation,
  • From Visual Studio's built-in integration, or
  • From a task on your build server (e.g., the npm task on Azure Pipelines).

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?.

like image 198
Jeremy Caney Avatar answered Oct 01 '22 14:10

Jeremy Caney