Question

I'd like to use the musicjson.js package that helps to convert musicXML files into json notation, looking if it's a good way to import for example exported musicXML Finale scores into a browser playing with the Fermata/VexFlow class.

https://github.com/saebekassebil/musicjson

The thing is that this module works with require (calling for nodes packages like fs) and I'm just a newbee in requirejs...Even if I spent few time in understanding the tutorial in the website, I don't still get how to solve this kind of basic problem when the dependencies of my musicjson.js need to be called like :

var xmldom   = require('flat-xmldom'),
fs = require('fs'),
path = require('path'),
util = require('util');

My index.php page does the classic require call:

<!DOCTYPE html>

 <head>

     <!-- javascript head -->
    <!-- REQUIRE -->
    <script data-main="scripts/main" src="bower_components/requirejs/require.js"></script>

</head>

  <body>

  </body>
</html>

In my scripts/main.js, I'd like to do simply what it is told from musicjon :

var music = require('musicjson');
music.musicJSON(xml, function(err, json) {
// Do something with the MusicJSON data
});

I putted also, in the same directory scripts/, the flat-xmldom folder, fs.js, path.js, util.js

When I do this, I've just obtain this classic error of : *Error: Module name "musicjson" has not been loaded yet for context: _. Use require([])*

...That looks like a common error referenced in the requirejs website, but if I try things that I guess it should be written, I get a bit lost to determine where is the fundamental conceptual mistake here :

require.config({
   baseUrl: '/scripts/',
   paths: {
    flatxmldom:'./flat-xmldom/__package__',
    fs: 'fs',
    path:'path',
    util:'util',
    musicjson: 'musicjson'
   }
});
require(['flatxmldom','fs','path','util','musicjson'],function(flatxmldom,fs,path,util,musicjson){})

Error returned in this case for example : *Module name "fs" has not been loaded yet for context: _. Use require([])*

Thanks a lot for your attention.

Was it helpful?

Solution

So, this is not a RequireJS problem per-se. The package you want to use is a Node.js package. It is intended to run in node (a server/desktop execution environment for JavaScript). It cannot/will not run in web page in a browser.

The packages it is trying to use (fs in particular) provide access to system resources such as the file system. Node provides these packages as part of its core libraries to any package that runs in node. A browser is specifically designed for security reasons never to allow direct access to such resources to any code that run in the browser because who knows where it came from or might try to do.

OTHER TIPS

I haven't really tried to do this myself, but browserify (alternative to requirejs) claims that it will allow you to use any node package in your application.

If musicjson is at the heart of what your app should achieve and requirejs is a small step on the way to getting there, you could try your luck with browserify instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top