JavaScript ReferenceError: exports is not defined

Martin.Jirsak's icon

Hi,

When I use the new Require way, I get the message: "JavaScript ReferenceError: exports is not defined", when I save the project with the "exports".

The JS fileis very simple:

exports.foo = 42;

Even if I get this error message, it works.

Any clue, why do I get this message, please (Max 7.3.3)? Thanks

michelez's icon

I don't think you need the export module. You can either include the whole js file with

include("jsfile"); // no js extension

or import individual functions from a js file like this

var somefunc = require("jsfile/somefunc");

of course the js files need to be in the search path. I put mine in:

Applications/Max/Contents/Resources/C74/extensions/jitter

so that Max can always find them.

I am no expert though so maybe someone else can chime in? (I had a related questions before but no answer...)

Martin.Jirsak's icon

Hi,

thank you for the answer.

I was following this article. This is, how I came to "export". I know about the "include" option, but then you "share" all variables, what is a bit risky. Thank you for the option to include one variable only, I will try this.

Simon O'Rorke's icon

Here's the fix that worked for me. When I got that error message, I had just converted a Javascript file from one that was hosted by a js object to one that exposed its functionality via exports/require. The error message was shown because I had not yet removed all the js objects that hosted the Javascript file from my project. When I removed those js objects, the error messages (on for each of the js objects ) were no longer shown.

So the rule, in Max at least, must be that a Javascript file containing exports must only be referenced by require, otherwise exports will not be understood and the error message will be shown.

If the cause of the error message was similar for Martin, that might explain why he found that the exports still worked, even though the error message was shown. The Javascript file would have worked where used in its correct context as a require file, but not where it was hosted by a js object (or in any other context).