1

Is there a way to import my own javascript module from a procedural proto node? I have a .js file with some function defined that I would like to use in several .proto files. The file is called configure_proto.js and it is in the same root folder than the protos.

When I try to import that module using import * as Config from 'configure_proto.js' it fails because it searches it in the temporal folder created by webots. I have also tried to dynamically copy the file to the temporary folder so that I can access it:

let protoPath = context.proto;
protoPath = protoPath.substring(0, protoPath.lastIndexOf('/') + 1);
let src = protoPath + 'configure_proto.js';
let dst = 'configure_proto.js';
wbfile.writeTextFile(dst, wbfile.readTextFile(src));
import * as Config from 'configure_proto.js';

This also fails with the following error:

error: Template engine error: failed to import JavaScript template: Could not open module file:///tmp/webots/jprieto/1234/configure_proto.js for reading

The interesting thing is that if I do not use the last line which imports the copied file, the code works and I can see that the file has been correctly copied to the folder, but there is no way to import it.

What can I do to solve this issue?

1 Answers1

0

Webots includes a number of Javascript modules which are included in some example PROTO files such as this one. These Javascript modules are stored in the WEBOTS_HOME/resources/web/wwi/protoVisualizer/templating/modules/webots/ folder. Adding your own Javascript files here will allow you to import them the same way as the original webots modules.

Ideally, we should improve Webots, so that it supports importing Javascript modules from the current proto folder and from the web. That should be pretty easy to implement. If you are interested doing this, feel free to go ahead with the implementation and open a new Pull Request that we will be happy to review.

Disclaimer: I am a Webots developer working at Cyberbotics.

Olivier Michel
  • 642
  • 5
  • 14
  • 1
    I wanted to avoid copying files, since the Javascript modules are in a git repository and they are always changing, so I have implemented a symbolic link between WEBOTS_HOME/resources/web/wwi/protoVisualizer/templating/modules/webots/ and my javascript folder. – Julieta Prieto Tarzia Jul 04 '23 at 06:40