External file in custom-module

Hi. Let’s say I have 1000 tracks in my DAW. Is it possible to define an object (e.g. a ‘tracks’ object) in a separate file, and import that into the custom module?

You can’t do that at run time for security reasons, but you could manage your sources in multiple files and bundle them into one custom module file before loading it in o-s-c.

The common pattern for this is using the browserify library:

  1. manage your tracks in tracks.js
module.exports = {
    // tracks definition
}
  1. load them in your main module file (we’ll call it main.js):
var tracks = require('./tracks.js')

module.exports = {
    oscInFilter: function(data){
        // etc
    }
}
  1. bundle your sources as custom-module.js :
    browserify main.js -o custom-module.js --standalone module.exports

Thanks for your reply. Bare with me here. So I installed node, then ran npm, then installed browserify. I created my two files (main.js and tracks.js) and then ran
browserify main.js -o custom-module.js --standalone module.exports

I got this back:

C:\Users\Luke\Desktop\node-v10.15.3-win-x64>browserify main.js -o custom-module.
js --standalone module.exports
Error: Parsing file C:\Users\Luke\Desktop\node-v10.15.3-win-x64\main.js: Unexpec
ted token (14:0)
    at Deps.parseDeps (C:\Users\Luke\Desktop\node-v10.15.3-win-x64\node_modules\
browserify\node_modules\module-deps\index.js:510:15)
    at getDeps (C:\Users\Luke\Desktop\node-v10.15.3-win-x64\node_modules\browser
ify\node_modules\module-deps\index.js:438:44)
    at C:\Users\Luke\Desktop\node-v10.15.3-win-x64\node_modules\browserify\node_
modules\module-deps\index.js:421:38
    at ConcatStream.<anonymous> (C:\Users\Luke\Desktop\node-v10.15.3-win-x64\nod
e_modules\browserify\node_modules\concat-stream\index.js:37:43)
    at ConcatStream.emit (events.js:194:15)
    at finishMaybe (C:\Users\Luke\Desktop\node-v10.15.3-win-x64\node_modules\bro
wserify\node_modules\readable-stream\lib\_stream_writable.js:630:14)
    at endWritable (C:\Users\Luke\Desktop\node-v10.15.3-win-x64\node_modules\bro
wserify\node_modules\readable-stream\lib\_stream_writable.js:638:3)
    at ConcatStream.Writable.end (C:\Users\Luke\Desktop\node-v10.15.3-win-x64\no
de_modules\browserify\node_modules\readable-stream\lib\_stream_writable.js:594:4
1)
    at DuplexWrapper.onend (C:\Users\Luke\Desktop\node-v10.15.3-win-x64\node_mod
ules\browserify\node_modules\readable-stream\lib\_stream_readable.js:577:10)
    at Object.onceWrapper (events.js:277:13)

Error: Parsing file C:\Users\Luke\Desktop\node-v10.15.3-win-x64\main.js: Unexpected token (14:0)

This means there is something wrong in main.js at line 14

My bad. I set up my definitions inside the module.exports!

You can see it this way: require('./file.js') returns the variable assigned to module.exports in file.js.

In v0.47.0 it is possible to read/write external json files from the custom module.

That’s cool! So what would this look like in my custom module if I have an external file tracks.js?

You’d have to name it tracks.json (loading javascript modules dynamically is still not allowed) and use the javascript object notation in it to define your data (see https://json.org/).
For example:


tracks.json:

{
  "track_1": {
      "name": "bass",
      "volume": 0
  }
}

custom-module.js:

var tracks = loadJSON('./tracks.json')
console.log(tracks.track_1)

module.exports = {
  // etc
}