Node-OPCUA Standard NodeSets
The `node-opcua-nodeset-ua` package is a core component of the `node-opcua` SDK, providing the standard OPC UA NodeSet XML definitions. These definitions are essential for building OPC UA servers that expose a semantically rich address space conforming to the OPC UA Unified Architecture specification. As part of the highly active `node-opcua` monorepo, it receives frequent updates, typically bi-weekly, aligning with the project's 'Perpetual Beta' model. The current stable version is 2.169.0. Key differentiators for the `node-opcua` ecosystem, including this module, include its pure Node.js implementation, extensive TypeScript support, and a focus on industrial IoT (IIoT) and Machine-to-Machine (M2M) communication. It allows developers to load standard and custom information models into an OPC UA server's address space for robust data exposure.
Common errors
-
ERR_REQUIRE_ESM: require() of ES Module ... not supported
cause Attempting to use `require()` to import `node-opcua-nodeset-ua` or other `node-opcua` modules in a CommonJS context, while the library is primarily designed for ES Modules.fixConvert your project or specific files to ES Modules by adding `"type": "module"` to your `package.json` or by using `.mjs` file extensions. Then, use `import { standardNodeSet2 } from 'node-opcua-nodeset-ua';` syntax. If a mixed environment is required, consider dynamic `import()` or a build step to transpile to CommonJS if strictly necessary. -
Bad_SecurityChecksFailed: The X.509 certificate is not trusted.
cause The OPC UA client or server has received a certificate from its peer that is not present in its trusted certificate store (PKI/trusted/certs folder) or is otherwise invalid/untrusted.fixEnsure the peer's certificate (or its Certificate Authority's certificate) is correctly placed in the `trusted/certs` directory of your client's/server's PKI store. For self-signed certificates, both sides typically need to trust the other's certificate. Verify certificate dates and revocation status. `node-opcua` provides tools (e.g., `node-opcua-pki`) to manage certificates. -
Cannot find name 'standardNodeSet2'.
cause The `standardNodeSet2` symbol (or similar NodeSet export) is either misspelled, or the import statement is incorrect, or the module structure has changed in an unexpected version.fixVerify the exact symbol name and the import path. The common import is `import { standardNodeSet2 } from 'node-opcua-nodeset-ua';`. Check the `node-opcua-nodeset-ua` package for available exports in your installed version. If using older `node-opcua` versions, the export name or module structure might differ. -
[node-opcua] Cannot load nodeset '...' : file not found or invalid XML format.
cause The NodeSet XML file provided to `server.loadUAModel()` is either missing, has an incorrect path, or contains malformed XML that prevents parsing. This can also occur if the file is ASCII-encoded when UTF-8 is expected.fixDouble-check the file path provided to `nodeset_filename`. Ensure the XML file is valid and correctly formatted, typically UTF-8 encoded, and accessible by the Node.js process. Tools like XML validators can help diagnose malformed files.
Warnings
- breaking Version 2.168.0 introduced a full migration of core packages from `async`/`lodash` to native modern JavaScript patterns. While primarily internal, this could affect users with custom plugins or extensions that directly relied on the removed third-party utilities, potentially causing breaking changes in their integrations.
- breaking With version 2.163.0, `node-opcua` finalized full support for OPC UA 1.05 subtyped structures and unions, coupled with a major overhaul of DataType handling. This significant change might cause issues for existing custom DataType definitions or information models that were implicitly relying on older, less compliant parsing behaviors.
- gotcha Certificate management APIs have undergone architectural overhauls and enhancements across several recent versions (e.g., v2.167.0, v2.164.2). Changes to `TrustListClient.addCertificate` and the `OPCUACertificateManager` API, including support for certificate chains, mean that older or non-standard certificate handling logic might behave unexpectedly or require updates.
- gotcha Introduced in v2.165.0, 'Advertised Endpoints' are crucial for correct operation in environments like Docker, behind NAT, or reverse proxies. Failure to properly configure advertised endpoints will lead to clients being unable to connect or discover the server correctly, as the internal network address may be exposed instead of the external one.
- gotcha The `node-opcua` client (since v2.123.0) now displays a warning `[NODE-OPCUA-W33]` if a significant time discrepancy (over 5 seconds) exists between the client and server clocks. While not strictly a breaking error, significant time differences can lead to security token renewal issues and unexpected disconnections in secure channels.
Install
-
npm install node-opcua-nodeset-ua -
yarn add node-opcua-nodeset-ua -
pnpm add node-opcua-nodeset-ua
Imports
- standardNodeSet2
const standardNodeSet2 = require('node-opcua-nodeset-ua').StandardNodeSet2;import { standardNodeSet2 } from 'node-opcua-nodeset-ua'; - standardNodeSet2Filename
import { standardNodeSet2Filename } from 'node-opcua-nodeset-ua'; - nodesets
import * as nodesets from 'node-opcua-nodeset-ua';
Quickstart
import { OPCUAServer } from 'node-opcua-server';
import { standardNodeSet2 } from 'node-opcua-nodeset-ua';
async function main() {
// Create an OPC UA Server instance
const server = new OPCUAServer({
port: 4840, // Default OPC UA port
resourcePath: '/UA/MyNodeSetServer',
buildInfo: {
productName: 'NodeSet Example Server',
buildNumber: '1',
buildDate: new Date(),
},
});
// Initialize the server
await server.initialize();
console.log('Server initialized.');
// Load the standard OPC UA NodeSet (version 2) into the server's address space
// The `standardNodeSet2` export provides the XML content as a string.
await server.loadUAModel({
nodeset_filename: [standardNodeSet2], // Pass the NodeSet XML string here
});
console.log('Standard NodeSet2 loaded.');
// Optionally, extend the address space with custom objects or variables
const addressSpace = server.engine.addressSpace;
if (addressSpace) {
const objectsFolder = addressSpace.getFolder('ObjectsFolder');
const myDeviceFolder = objectsFolder?.addFolder({
browseName: 'MyCustomDevices',
displayName: 'My Custom Devices',
});
myDeviceFolder?.addVariable({
browseName: 'Temperature',
dataType: 'Double',
value: { get: () => new server.variant({ dataType: server.DataType.Double, value: 25.5 }) },
});
}
console.log('Custom objects added to address space.');
// Start the server
await server.start();
console.log(`Server is now listening on port ${server.port}`);
console.log(`Server discovery URL: ${server.getEndpointUrl()}`);
console.log('Press Ctrl+C to stop the server.');
// Handle server shutdown on SIGINT
process.on('SIGINT', async () => {
console.log('Shutting down server...');
await server.shutdown();
console.log('Server shut down.');
process.exit(0);
});
}
main().catch(console.error);