Node.js / TypeScript · recipe
Call an OPC UA method in Node.js
Call takes the owning object's node id, the method's node id and an array of typed input arguments. The result carries its own status code and the output arguments, so check the status before using them.
| Stack | node-opcua |
|---|---|
| Version | 2.181.1 |
The code
const { OPCUAClient, DataType } = require("node-opcua");
(async () => {
const client = OPCUAClient.create({ endpointMustExist: false });
await client.connect("opc.tcp://milo.digitalpetri.com:62541/milo");
const session = await client.createSession();
const result = await session.call({
objectId: "ns=2;s=Methods",
methodId: "ns=2;s=Methods/sqrt(x)",
inputArguments: [{ dataType: DataType.Double, value: 16.0 }],
});
console.log("status :", result.statusCode.toString());
console.log("outputs:", result.outputArguments.map((a) => a.value));
await session.close();
await client.disconnect();
})();
What it prints
status : Good (0x00000000)
outputs: [ 4 ]
Notes
- You need both objectId and methodId. Browse the object to find the method rather than assuming its node id follows a pattern.
- Every input argument needs an explicit dataType. This is the same JavaScript number problem as writes.
- The result has its own statusCode, separate from the transport. A method can be reached and still fail.
- Read the method's InputArguments property to get the declared count, order and types before calling.
What breaks first
The call fails with an argument error even though the value looks correct
Read the InputArguments property on the method node and match its declared types and order exactly, including scalar versus array.
BadInvalidArgumentRead the InputArguments property on the method node and match its declared types and order exactly, including scalar versus array.
Other recipes in this stack
Concepts on this page
Last verified 7 September 2026 against OPC UA 1.05.07. OPC UA Connect is an educational resource and is not affiliated with the OPC Foundation.