Reference error: 'x' is not defined

Windows 10
PsychoPy v2021.1.2

I’m currently trying to pilot a learning task experiment on Pavlovia to make sure it’s working. It’s a task I made on PsychoPy. It runs just fine locally.

However, when I reach the feedback part of the first trial (when it should display a message), Pavlovia doesn’t like the msg variable. Specifically, it gives me the following error: ReferenceError: msg is not defined. It’s obviously defined in my code under ‘begin experiment’, and it works fine on my own machine.

I first changed it from mesg to msg in case ‘mesg’ referred to something specific in JS I wasn’t aware of, but to no avail. I know there are similar enquiries already, but I didn’t find any of their solutions to work.

I wonder if anybody has any possible solutions that could resolve it? I can share the url of my experiment:

Many thanks.

This tutorial might help you find the problem: Thomas Pronk / tutorial_js_semantic_error · GitLab

Thanks, that was a useful read! I wasn’t aware you could do that.

It shows me line 1073:


However, I’m not sure how to rectify the error. Inside the code elements of the builder, I’ve defined msg:

msg = 0

in preparation for it being changed in my feedback to a reward message. Even if I do so before it’s called on in the feedback routine (e.g. in the experiment’s instructions), it seems to produce the same result of the error on Pavlovia.

Is your msg text set to every repeat or constant? The latter may give the error

I just double checked and the text is indeed set to every repeat


do want to return an integer msg = 0 or a string msg = "0" in your feedback?

Best wishes Jens

HI Jens,

A string. I defined it as msg = '' which was effective when running the experiment (i.e. it did not return any errors about it being undefined and worked as intended). The only thing it will ever say other than ' ' is Correct! You won 15 points. which is output as part of a feedback routine as a text component in the builder.

The intention is for the text to either show nothing or to show the reward feedback.

Many thanks.

Please could you show a screenshot of where you are defining and/or setting msg. The only time I can see it appear in your JS code is feedbacktextrab.setText(msg);

I’ve defined it in a code element in the builder view, but I also couldn’t find it inside the JS code. I’ll link a screenshot.

Is the issue that I have to manually insert it into the script compiled by PsychoPy?


That code component is set to Py. Change it to Auto to create JS code.

I did originally use the auto-JS when I first uploaded, but it produced the same error. I just published the same study with auto enabled, but the reference error seems to happen regardless

I can still only see msg once in your JS code

Are you getting the green sync confirmation? Are you syncing a different file.

Yep, confirmation on the sync.

I wonder, would you suggest manually inserting the msg = ' ' in the script somewhere before the first instance of msg in current JS code? Though none of the other variables seem to suffering by me defining them in the code element.


well msg is not defined in your JS as @wakecarter stated which you can check by searching the JS-code for msg. Do you still use the variables following leanReps, e.g. richCorrCount aso.? I assume not because not one of these variables appear in your JS-code.

You could add msg to your JS-code but this would be overwritten by any changes you make locally.

Best wishes Jens


I modified the code such that the previously unaccounted variables are defined within the JS code uploaded to Pavlovia. However, Pavlovia still seems to be pulling from the previous iteration of the code. With this script inside my project on the site, I get the same reference error and analysing the resulting code confirms it’s not defined. Apologies if I’m making a silly error, but am I making an obvious mistake somewhere?


I would strongly discourage you from directly editing your JS file.

If you have msg = " " in an Auto component you will get msg = " "; in the JS side. If that’s not getting to your JS code then that’s the issue you need to solve.

Hello James,

do you mind give access to your repo or to upload your experiment or a toy version of it that one could run locally?

Best wishes Jens

Hi Jens,

Sure, it has a set of stimuli which I’ll also need to include for it to run locally, so I will send that in a message if that’s okay.

Hello James,

do not import Pthyon-libraries. PsychoPy imports all libraries available in PsychoJS for you. There was a typo in your feedback-code: mesg instead of msg. Use an additional loop around your trials instead repeating it three times (PsychoPy gives you a warning that you are duplicating variables).


There were ’ in front of Smile*.png as well q and p in the Excelfile.

Any reason why you start registering the response after stimulus onset instead of at stimulus onset?

You use a newer PsychoPy-version than I do. Your experiment does not compile with my version (2020.2.10) as the image-component has a new parameter (contrast) that the older version does not have.

Best wishes Jens

1 Like


BTW, the way you check for the correctness of the answer will not work, at least not the version you send me.

Try this in the Begin routine tab of your code component (assuming your keyboard-component is called key_resp):

if not key_resp.keys:
    msg="Failed to respond"
elif key_resp.corr: 
    msg='Wrong X!'

If you set msg to msg = "doh!" in the start experiment tab of your code-component, you notice when the if-construction is evaluated.

All taken from here.

Best wishes Jens