One of the newest “big things” is building a web application. People want their applications accessible no matter what the platform in an easy-to-learn format. However, sometimes web developers don’t like working with just JavaScript. Sometimes, you’ll want something more fun such as Lua. This guide will help you write an Electron.js (Electron Shell, used for software such as Tofino, Atom, and Visual Studio Code) or web application using Lua as an assistant. Here is a link to the Lua.vm.js GitHub repository..

Website Application using Lua.vm.js “Web” version

Building an app using Lua is as simple as loading the Lua.vm.js web version and building a <script> for it. Example below.

Web-based Application using Node.js, Electron.js, and Lua.vm.js

This is not intended as an Electron tutorial, only how to use Lua with Electron or Node.js

Building an Electron application is a bit more difficult because you’ll need to install the Node.js version; however, using Node.js, you can install both Lua.vm.js and Electron.js in two simple commands.

$ npm install lua.vm.js

$ sudo npm install electron-prebuilt -g

Installing Electron.js in a global location allows for it to be automatically added into your path; if you would like to instead run Electron from the current location:

$ npm install electron-prebuilt

$ PATH="$(npm bin):$PATH" electron

Using this local format can be written as a simple sh function:

npm-exec() {

PATH="$(npm bin):$PATH"

$*

}

This way, you can run `npm-exec electron` and even if you have it installed locally it’ll run Electron.

To use Lua inside of your Node.js project, you need to load the VM, create a new Lua state, and execute some code.

var LuaVM = require("lua.vm.js"); // Load Lua VM

var L = new LuaVM.Lua.State(); // Create new Lua state

L.execute('print("Hello World!")'); // Execute Lua code

To execute Lua code, you would need to load the file in Node.js, read all of the code, and pass it to the Lua state’s execute function. Alternatively, you could put the Lua code inside of the loaded HTML file like the above example.