Using JS Module with RequireJS

Using JS Module with RequireJS

de Aina Palacios -
Número de respuestas: 1

Hi! I have been searching for information to use a JS node_module that needs to be required in my plugin. I have followed the instructions for an external javascript/jquery library from https://docs.moodle.org/dev/Javascript_Modules and from https://docs.moodle.org/dev/Guide_to_adding_third_party_jQuery_for_AMD, but it didn't work.

I came to the conclusion that I need to use AMD, so I created a file named config.js inside the folder /amd/src:

define([], function () {
window.requirejs.config({
paths: {
           "sentimentM": M.cfg.wwwroot + '/local/analysentiment/amd/build/node_modules/sentiment/build/build.min'
},
shim: {
            'sentimentM': {exports: 'sentimentM'}
}});});

I want to use the library sentiment (https://www.npmjs.com/package/sentiment). I placed this library in a /js folder and also I tried to add it to the /amd/src folder and use the build path, as the example above. 

Then, I created a file at /amd folder named sentimentM.js:

define(['local_analysentiment/config', 'sentimentM'], function(unused,SM) {  return SM; });

And finally, a file named analysentiment.js at /amd/src:

define(['local_analysentiment/sentimentM'], function (Sent) {
function initManage() {
var sentiment = new Sent();
var result = sentiment.analyze('Cats are stupid.');
window.console.dir(result);
}
return {
init: function () {
window.console.dir('init');
initManage();
}};});

I use grunt to compile it and in my index.php I add the following line:
$PAGE->requires->js_call_amd('local_analysentiment/analysentiment', 'init');

Now, I have the following error:
  • require.js:1961 GET http://localhost/turisme/lib/requirejs.php/-1/sentimentM.js net::ERR_ABORTED 404
  • require.js:168 Uncaught Error: Script error for "sentimentM", needed by: local_analysentiment/sentimentM http://requirejs.org/docs/errors.html#scripterror at makeError (require.js:168:17) at HTMLScriptElement.onScriptError (require.js:1738:36)

I tried too many things: adding also to my PHP file the other js files (although they seem to compile alone), using other requirements or definitions, pray... At some point, the error requires also "async", and I could manage it; but then the error was that I need "fs" and I have no idea of what to do! And also I don't think this is a solution :C.

Any clue of what to do? Any functional examples?  


Promedio de valoraciones: -
En respuesta a Aina Palacios

Re: Using JS Module with RequireJS

de Andrew Lyons -
Imagen de Core developers Imagen de Moodle HQ Imagen de Particularly helpful Moodlers Imagen de Peer reviewers Imagen de Plugin developers Imagen de Testers
Hi Aina,

It looks like Sentiment is a NodeJS module, and is not suitable for the browser. It's also using the CommonJS format, which I don't think we currently support, though it's a long time since I've had any need to try this so I may be wrong.

It does look like it's a fairly small library and it would be quite easy to create an ESM using the same built data (build/emoji.json), or to transpile it in some way.

Best wishes,

Andrew