Require is not working for node-opcua

357 Views Asked by At

I want to load a local version of node-opcua with 'require' inside a HTML file, but it does not really work. The code snippet is the following:

<script type="text/javascript" src="path_to_require.js"></script>

<script> 

var opcua = require(["path_to_node-opcua"]); <!-- Yes, the path is correct >

var client = new opcua.OPCUAClient();

...

When I execute the script I get the following error in the console:

Uncaught TypeError: opcua.OPCUAClient is not a constructor

Hence, var opcua is loaded correctly, but OPCUACluent is not, although the class is declared in a file that is present in the node-opcua folder called opcua_client.js under node-opcua\lib\client\

Sources: The 'require' script from http://requirejs.org/docs/download.html#requirejs. The node-opcua folder with the console command npm install node-opcua.

2

There are 2 best solutions below

0
On BEST ANSWER

node-opcua is not intended to run inside a browser as it relies on nodejs specific features such as filesystem access, crypto and so on.

1
On

You need to use browserify if you want to use that module in client. You will also need to look at how to use browserify with file system access (it can be done if paths are known ahead of time).