Using nodeJS modules

Using nodeJS modules

by Nikita Lvov -
Number of replies: 5

Hello,

I wanted to use a nodeJS module in my plugin, but do not know how to define it. 

I've installed the module with npm isntall. Now it located in the node_modules folder.

For the other JS files I define them in AMD style like 

define(["block_my_plugin/ScriptToUse"], function (ScriptObj) {
...
}




Average of ratings: -
In reply to Nikita Lvov

Re: Using nodeJS modules

by Nitesh Kumar -

Hi,

Did you find the solution? I also want to use node in my plugin.

In reply to Nikita Lvov

Re: Using nodeJS modules

by Alexandre Ferreira -
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.