Skip to main content
Roberty Studio
RPATutorials

Installing external Node.js libraries

Installing external Node.js libraries

Roberty ships with a good many JavaScript libraries already available in the JavaScript code action (axios, lodash, moment, fs-extra and plenty more). When you need one that is not included, you can install it by hand and use it normally in your code.


How it works​

Every compiled robot has a folder of its own inside Roberty's AppData:

C:\Users\{User}\AppData\Roaming\roberty-v4\robots\{robot-id}\

That folder holds the compiled index.js and a node_modules folder where the dependencies live. Any package installed there is available to the robot's JavaScript.

Roberty uses its own Node.js (version 22), at:

C:\Users\{User}\AppData\Roaming\roberty-v4\vendor\node\22.16.0\

Step 1 — Compile the robot​

Before installing anything, the robot has to have been compiled at least once, so that its folder exists.

  1. Open the robot in the Designer.
  2. Click Compile.

Step 2 — Find the robot's folder​

  1. Open File Explorer and go to:
    C:\Users\{User}\AppData\Roaming\roberty-v4\robots\
  2. The folders are named after the robot's ID. Identify the right one by its modified date — the most recent is the robot you just compiled.
Shortcut

Paste %APPDATA%\roberty-v4\robots\ straight into File Explorer's address bar and press Enter.


Step 3 — Open a terminal in the robot's folder​

  1. With the robot's folder open in File Explorer, click the address bar.
  2. Type cmd and press Enter to open a Command Prompt in that directory.

Step 4 — Install the library​

Use the npm that comes with Roberty:

bat
"%APPDATA%\roberty-v4\vendor\node\22.16.0\npm.cmd" install package-name

For example — installing cheerio for HTML parsing:

bat
"%APPDATA%\roberty-v4\vendor\node\22.16.0\npm.cmd" install cheerio

Wait for npm to download and install it. The robot folder's node_modules then contains the package.


Step 5 — Use the library in your code​

In the JavaScript code action, import the package with a dynamic import (import()). The robot's JavaScript runs in an ES module context, so require() is not available.

javascript
const { load } = await import('cheerio')

const html = actions["fetchPage"]["value"]
const $ = load(html)
const title = $('h1').text()

return title
Why import() and not require()?

The code Roberty compiles uses ES modules. Inside an ES module, require() does not work — always use the dynamic await import('package').

Common dynamic-import shapes​

javascript
// Default export
const { default: packageName } = await import('package-name')

// Named exports
const { functionA, functionB } = await import('package-name')

// Everything in one object
const module = await import('package-name')
module.someFunction()

Recompiling does not remove your packages​

When the robot is recompiled, Roberty runs npm install to guarantee its own internal dependencies, but it does not remove extra packages you installed by hand. Yours stay in node_modules across recompiles.

The exception: a deleted robot folder

If you delete the robot and create it again, or the folder is removed by hand, the packages have to be installed again.


Troubleshooting​

import() returns "Cannot find module". Check the package went into the right folder — the robot's, not somewhere else. Confirm that the node_modules inside the robot's folder contains it.

npm.cmd returns a permission error. Run the Command Prompt as Administrator and try again.

I do not know which folder is my robot's. Recompile the robot and look for the most recently modified folder under %APPDATA%\roberty-v4\robots\.


Next​