Skip to content

Setting up Node.js module

Locate jsworker folder

Navigate to the writable directory of your Omnis Studio installation, typically located inside the AppData folder on Windows or the Application Support folder on macOS. On Linux, the writable and read-only directories are the same.

Look for a folder named jsworker. This is where all the modules that can be used with the JavaScript Worker will be installed.

Create module folder

Create a new folder inside jsworker and name it after your module. The folder's name will be used to identify your module when calling its methods through the JavaScript Worker from Omnis Studio.

For example, if your module is named test_module, create a folder named test_module inside the jsworker directory.

Create package.json

Inside your module's folder, we need to set up a package.json file describing the module's version, name, dependencies and most importantly, the entrypoint.

The easiest way is to use npm init from a terminal or command prompt which will ask you a bunch of questions, and then write a package.json for you. Alterntively, you can create the file manually with the following contents:

package.json
{
  "name": "your_module_name",
  "version": "1.0.0",
  "main": "test.js"
}

"name" is used by the package manager to identify modules. Omnis Studio uses the names of the folders inside jsworker to identify modules, but it must be defined for a valid package.json.

"version" is used by the package manager to identify your module's version. Omnis Studio does not currently use the version of a module, but it also must be defined for a valid package.json.

"main" is of most importance to Omnis Studio as it defines the entrypoint of your module. For example, if your package.json defines "main": "test.js", Omnis Studio will look for a test.js file inside your module's folder (at the same level as your package.json) when calling a method through the JavaScript Worker.

Feel free to modify any of the options above to suit your needs.

Create entrypoint file

Inside your module's folder, create a new file and use the value from "main" from your package.json as its name.

For example, if your package.json defines "main": "test.js", your file must be named test.js.

The entrypoint file is responsible for exposing your module's methods to Omnis Studio's JavaScript Worker through a call function.

Here is an example of a simple entrypoint file. It contains a method map with one method and exports a call function, which enables the invocation of any function within the method map.

test.js
const omnis_calls = require("omnis_calls");
const { errorCodes, newErrorWithCode } = require("../errors.js");

const methodMap = {
  test: function (param) {
    return {
      hello: "world",
    };
  },
};

module.exports = {
  call: function (method, param, response) {
    if (methodMap[method]) {
      const outcome = methodMap[method](param, response);
      omnis_calls.sendResponse(outcome, response);
      return true;
    } else {
      throw newErrorWithCode(errorCodes.METHOD_NOT_FOUND, "");
    }
  },
};