How to Use Node.js with JavaScript in Combination with UiPath

Node.js is a JavaScript runtime environment on Chrome V8 engine. JavaScript is a very common and used programming language from many programmers around the world. A variety of packages are offered for Node.js that provide different aspects of problem solving. The consideration of an integration scenario into an UiPath RPA workflow can only be profitable from these points of view.

It is possible to execute JavaScript code with Node.js or to use it in REPL (read-eval-print-loop) mode.

To realize a smart integration of Node.js with JavaScript into UiPath I have developed an activity. This activity works with compatibility modes Cross-platform (x64), Windows (x64) and Windows - Legacy (x86).

NodeJS.Activities.1.1.0.nupkg (20.6 KB)

image

With this it is possible to use JavaScript code via Node.js runtime environment easily.

image

To use this activity it is necessary to download Node.js and to install it on the target system first. Afterwards it is possible to set an environment variable NODE_HOME with the path to Node home directory. If the variable is not set it is necessary to set in the Options the NodeJSPath property to Node executable.

image

Here a tiny example which reads the input arguments and delivers it back.

//-Begin----------------------------------------------------------------

var myArgs = process.argv.slice(2);

if(typeof myArgs[0] == 'undefined') {
  console.log("Hello World");
} else {
  console.log("Hello " + myArgs[0]);
}

//-End------------------------------------------------------------------

The output is performed using the log method of the console class. This prints to stdout and is captured by the activity.

image

On this way we can seamlessly execute JavaScript source code in UiPath. For certain use cases this can be very advantageous.

JSON Returning

Very often in the JavaScript programming environment, results are returned in JavaScript Object Notation (JSON). This should be used without any problems.

//-Begin----------------------------------------------------------------

"use strict";

console.log(process.versions);

//-End------------------------------------------------------------------

image

To use this JSON return with UiPath.WebAPI.Activities it is necessary to convert the value to a JSON string in square brackets.

//-Begin----------------------------------------------------------------

"use strict";

console.log("[" + JSON.stringify(process.versions) + "]");

//-End------------------------------------------------------------------

Conclusion

Using Node.js with JavaScript opens further integration possibilities for UiPath, besides the possibility of using JavaScript as programming language. In particular, the using of network applications in the browser can be easily realized with it. This makes it very easy to implement local scenarios for test automation in this case. And all this in a very handy size.

13 Likes

Hello!
Thanks for Node.js in UiPath)
Very useful!
Please tell me how to pass an argument to Invoke Javascript activity?
I have a string variable and I need to process with JS.
I standardly create an argument, assign it the value of a string variable and specify in the properties Input and my arguments, but I get an error:
“ReferenceError: out_variableName is not defined”(
What am I doing wrong?

1 Like

@Valeriy

Hello Valeriy,
welcome in the UiPath Community.

I tried this sequence …

image

… with this code in the file TestScript.003.js …

//-Begin----------------------------------------------------------------

// Hello from Node.js 18.9.0
console.log(`Hello from Node.js ${process.versions.node}`);

var myArgs = process.argv.slice(2);

myArgs.forEach((val, index) => {
  // 0: String with Spaces
  // 1: 1
  // 2: 2.5
  console.log(`${index}: ${val}`);
});

var Arg1 = myArgs[0];
// String with Spaces
console.log(`${Arg1}`);

//-End------------------------------------------------------------------

… with this properties …

image

… and this result.

image

I use VB language convention.

image

The arguments are a string in double quotes, an integer and a float. To get access to the content of the command-line arguments use process.argv. process.argv delivers all arguments as string[].

Best regards
Stefan

1 Like

Hello Stefan.
Thanks you!
I got it all)
And one more question if possible?!
Can i pass several variables into an activity?

1 Like

@Valeriy

Hello Valeriy,
sure, look at my example above, three different types of variables.

If you want to pass three strings, with VB convention, try this in the input of the arguments:

"""String1"" ""String2"" ""String3"""

And in Node.js try this:

var myArgs = process.argv.slice(2);
// String1
var Arg1 = myArgs[0];
// String 2
var Arg2 = myArgs[1];
// String 3
var Arg3 = myArgs[2];

Best regards
Stefan

Stefan, I apologize for my newbie questions!)
You wrote how to pass multiple text strings, but can you write how to pass multiple variables to process JS and get them back?

@Valeriy

Hello Valeriy,
there is no difference, if you use the following parameters, instead of “”“String1"” "“String2"” ““String3"””, …

"1 2 3"

… in Node with this JavaScript source …

//-Begin----------------------------------------------------------------

var myArgs = process.argv.slice(2);

// 1
var Arg1 = myArgs[0];
// 2
var Arg2 = myArgs[1];
// 3
var Arg3 = myArgs[2];

// Convert string arguments to number
var Num1 = parseInt(Arg1);
var Num2 = parseInt(Arg2);
var Num3 = parseInt(Arg3);

// Returns 6 1 2 3 to UiPath
console.log(`${Num1 + Num2 + Num3} ${Arg1} ${Arg2} ${Arg3}`);

//-End------------------------------------------------------------------

… you get this result.

console.log delivers one string with 4 numbers and it is possible to split it at the space to get the single numbers.

Best regards
Stefan

Hi Stefan - Can we use this activity to:

  1. Return a value as Output and store it into a variable for further use?
  2. I see that Datatype of Output is String. Can we return other type like Object using JS code?

Thanks,
Anuj