

The ClearScript library takes care of the P/Invoke calls, providing a nice C# API, but you still have to make sure you're deploying the correct native libraries based on your target platform.

Just as with ChakraCore, the V8 engine itself is a native dependency. The Microsoft.ClearScript package provides a wrapper around the library, providing a C# interface for calling into the V8 library. The V8 JavaScript engine is what powers Node.JS, Chromium, Chrome, and the latest Edge. But it can provide some deployment challenges. So ChakraCore includes a native dependency, but as C# can P/Invoke into native libraries, that's not a problem per-se. It can be compiled for 圆4 processors on Linux macOS and Windows. According to the GitHub project:ĬhakraCore is a JavaScript engine with a C API you can use to add support for JavaScript to any C or C compatible project. ChakraCoreĬhakraCore was the original JavaScript engine used by Microsoft Edge, before Edge moved to be based on Chromium. This can be useful in environments where you know both are installed, but it doesn't really solve the logistics problem I was trying to avoid. Instead, it provides a C# API for executing JavaScript code, and it calls out to the Node.js installed on your machine. It doesn't include Node.js in the package. This library takes the first of the above approaches. The following is a collection of just some of the packages you could use to. This isn't quite as crazy as it sounds, and there are several NuGet packages that take this approach, which then expose a C# layer for interacting with the engine. You could shell-out to a JavaScript engine (like Node.js) and ask it to run your JavaScript for you, but then you haven't really solved the problem you would still need Node.js installed.Īnother option is to bundle the JavaScript engine inside your library directly. NET code, a couple of options come to mind. Once you've accepted that you want to run JavaScript from your. NET app 🤯 Approaches to running JavaScript inside. Or… we call JavaScript directly from our. We can either go the C# (+ native) route, or we have to jump out to JavaScript. That seems like a big overhead just to use one small feature. NET developer is that you need to learn and opt in to a whole separate tool chain, working with Node.js and NPM.

The obvious trouble with JavaScript as a. The first two, in particular, are very mature, with multiple plugins and themes, and with simple APIs. To name a few, there's highlight.js, Prism.js (used on this blog), and shiki.js. In contrast, JavaScript has a plethora of mature syntax highlighting libraries. However it wraps a native dependency which adds some complexities if you're looking to deploy the app.
#Runjs alternative code
These are the files that VS Code uses to add basic syntax highlighting for a language. The TextMateSharp project, for example, provides an interpreter for TextMate grammar files. This is possible to do with C# directly, but it's not an especially smooth experience. One of those things is having a library for everything, especially when it comes to the web. NET ecosystem, there are some things that the JavaScript ecosystem just does better. Madness right? It's actually surprisingly easy! Why would you do this?Īs much as I like the. The thought of dealing with Node.js and npm again totally put me off, so I decided to look into the possibility of running JavaScript inside a. I was working on a side project the other day, and realised I really needed to use some JavaScript functionality.
