Using nodeJS modules

Re: Using nodeJS modules

by Alexandre Ferreira -
Number of replies: 0
Hi there,

I wanted to import a nodejs module in my plugin as well and I've been cracking my head for a few days now. In my case I wanted to access the functionalities of a parser library (https://pegjs.org). I've tried the tutorial that is given in https://docs.moodle.org/dev/Javascript_Modules in the topic of exporting external libraries. I followed all the setps that were given but I still couldn't import the library due to requirejs error. I'm not an expert in Javascript modules but after reading some documentation in requirejs I thought that the js files that I was using weren't AMD compatible. But I'm pretty sure they are after reading the pegjs's documentation. And then I noticed that none of the modules placed in 'node_modules' couldn't actually be loaded if you called them in a script without having some conflict with requirejs. So what I did that solved me the problem was configuring requirejs file. I created a folder in 'moodle/lib' and copied the two files indicated in the moodle's tutorial that I mentioned above and added the path to my new folder in requirejs file. However, I'm pretty sure that this isn't the way it's supposed to work. As I said before, I'm not an expert in the materia.

Hope you came up with a better solution.