Getting started

From npm install to a working OPC UA client in under 5 minutes. This guide uses TypeScript and ES modules.

01

Install

Create a new project and install node-opcua:

mkdir my-opcua-app && cd my-opcua-app
npm init -y
npm install node-opcua
npx tsc --init

node-opcua has zero native dependencies — npm install is all you need. TypeScript types are included out of the box.

02

Read from a server (client)

Create client.ts and connect to a public demo server. This reads the server's current time:

import { OPCUAClient, AttributeIds } from "node-opcua";

const endpointUrl = "opc.tcp://opcuademo.sterfive.com:26543";

const client = OPCUAClient.create({ endpointMustExist: false });

await client.withSessionAsync(endpointUrl, async (session) => {
  const dataValue = await session.read({
    nodeId: "ns=0;i=2258", // Server CurrentTime
    attributeId: AttributeIds.Value,
  });
  console.log("Server time:", dataValue.value.value);
});

withSessionAsync handles connect, session creation, and clean disconnect automatically. The demo server at opcuademo.sterfive.com is publicly accessible.

03

Create a server

Create server.ts with a Temperature variable that returns a random reading on every read:

import { OPCUAServer, DataType } from "node-opcua";

const server = new OPCUAServer({
  port: 4840,
  resourcePath: "/UA/MyServer",
  buildInfo: { productName: "MyServer", buildNumber: "1" },
});

await server.initialize();

const namespace = server.engine.addressSpace!.getOwnNamespace();
const device = namespace.addObject({
  organizedBy: server.engine.addressSpace!.rootFolder.objects,
  browseName: "Device",
});

namespace.addVariable({
  componentOf: device,
  browseName: "Temperature",
  dataType: "Double",
  value: {
    get: () => new Variant({
      dataType: DataType.Double,
      value: 19 + Math.random(),
    }),
  },
});

await server.start();
console.log("Server listening on", server.getEndpointUrl());

Your server is now browsable by any OPC UA client — UaExpert, Prosys, or another node-opcua client.

04

Run it

npx tsx client.ts
# → Server time: 2025-07-05T21:08:00.000Z

npx tsx server.ts
# → Server listening on opc.tcp://localhost:4840/UA/MyServer
05

Next steps

  • API reference — full documentation for every class and method
  • GitHub repo — examples, issue tracker, contribution guide
  • The book — "node-opcua by Example" (2024 edition)
  • Ecosystem — explore the full family of node-opcua packages
  • Commercial support — SLAs, consulting, and PRO modules from Sterfive