Problem initialising online experiment

url experiment:
project page:
Browser: chrome

Hi guys,

I created an experiment in the Builder (v2020.2.5). I am trying to run my experiment online for the first time. My experiment gets stuck at the beginning; ‘initialising the experiment’.

The console shows the following error: ‘Uncaught SyntaxError: Unexpected token ‘*’’, which refers to the following lines:

import * as random from ‘random’;
var condition;
condition = random.choice([“A”, “B”, “C”, “D”, “E”, “F”, “G”, “H”]);

I know import functions are often not working in JS, so in my Builder file I wrote some code in JS myself:
function random_character() {
var chars = “ABCDEFGH”;
return chars.substr( Math.floor(Math.random() * 8), 1);

condition = random_character();

It however seems from the console that the JS code is being autoconverted back to Py. But perhaps I am missing something else here.

Does anyone have an idea how to solve this issue with initializing?

Thanks very much in advance!


I use the following code to generate a random string vpcode online:

characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
charactersLength = characters.length;
vpcode = " "
for (var i = 0; i < 9; i++) {
    vpcode += characters.charAt(Math.floor(Math.random() * charactersLength));

If you edit JavaScript set the code-element to both or create a JavaScript JS only code-element.

As you noted it the line ‘import * as random from ‘random’;’ in row 251 of your script seems to cause the error.

Cheers Jens

Dear Jens,

Thanks very much for your reply and for helping me with this problem.

I am not sure if there is an issue with my previous written JS code, because when using this code (within the options “Both” and “JS”), my experiment was running fine locally; the code randomly picks one character as I intended.

For the online experiment, I selected the “JS” option in the builder, but in the script (as seen in the Console) it seems that the code is being autotranslated back to Py, is that possible? Is there anything I could do to change this?



your JavaScript code-element does not matter offline (locally). Offline the python-part is of relevance, online it is the JavaScript-part.

Whenever to change the setting from JS to Py, your JS code it “deleted”. For this reason, I either leave the setting set to both at all times or simply insert a JS-only code-element.

See the following example which has a python and JS code-element:

content JS code-element

content python code-element

As you can see, both code-elementes contain quite different code. I do not need to detect a mobile device offline, for instance :smiley:

I need the python vpkode-code to run the expeiment locally because vpkode will be used later in the experiment. But as a matter of fact I would not need it if I run the experiment only offline.

I do not understand what you mean with

There was an issue.

caused an error.

Best Jens

Hi Jens,

Thank you so much for your elaborate response!
I put different JS and Py code in the builder.

JS code =
function random_character() {
var chars = “ABCDEFGH”;
return chars.substr( Math.floor(Math.random() * 8), 1);
condition = random_character();

I however realised now that actual problem is that the online task is trying to use an old js-script, because there are some problems with exporting the html code of the current version of the experiment.

I think there are still some problems with ‘condition’ (see traceback below). I tried your JS code as well, but the issue is still there. The task works fine offline.
The code randomly picks a character, and this character refers later on to a specific task Version (name of loop for instance: 'Version' + condition + '_Precond_USrating.xlsx'). I have a few double loops (inner and outer loop) by which the order of two blocks is randomly selected (outer loop for example ‘Version’ + condition + ‘_selectBlock_conditioning.xlsx’); inner loop refers to a condition (condfile_conditioning) that is specific in the (‘Version’ + condition + ‘_selectBlock_conditioning.xlsx’)-file).

I was wondering if you have an idea what error the Traceback code refers to and how the errors can be solved.

Many thanks and best,


Traceback (most recent call last):
File “/Applications/”, line 782, in findPathsInFile
File “”, line 1, in
NameError: name ‘condition’ is not defined

During handling of the above exception, another exception occurred:
File “/Applications/”, line 1221, in onPavloviaSync
File “/Applications/”, line 719, in fileExport
File “/Applications/”, line 73, in generateScript
File “/Applications/”, line 242, in compileScript
File “/Applications/”, line 214, in _makeTarget
File “/Applications/”, line 213, in writeScript
File “/Applications/”, line 290, in writeFlowSchedulerJS
File “/Applications/”, line 830, in getResourceFiles
File “/Applications/”, line 796, in findPathsInFile
File “/Applications/”, line 782, in findPathsInFile
File “”, line 1
SyntaxError: invalid syntax

Hello Iris,

it would be helpful if you surrounded code-elements with triple ` It makes reading the code easier and allows finding indentation errors.

function random_character() {
var chars = “ABCDEFGH”;
return chars.substr( Math.floor(Math.random() * 8), 1);
condition = random_character();


function random_chararcter(){
    var chars = "ABCDEFGH";
    return chars.substr( Math.floor(Math.random() * 8), 1);

condition = random_character();

The traceback shows you that the variable condition is not defined. If you want to use the variable condition as a pointer to your various file you have to proceed it with a $. So, it is
‘Version’ + $condition + ‘_selectBlock_conditioning.xlsx’

BTW, I am not sure whether it is necessary to define a function to randomly select a letter. So, you might be better off this way:

characters = [A,B,C,D,E,F,G,H];
condition = characters[0];

This will pick the first element of the randomized array characters.

You need to define shuffle, best at the beginning of the experiment in a JavaScript code-element:

shuffle = util.shuffle;

You need an error-free code (syntax) before being able to synchronize it.

Best Jens

Hi Jens,

Many thanks for your quick reply!

I made some changes according to your suggestions, but I got a new error referring to the ‘Version’ + $condition + ‘_selectBlock_conditioning.xlsx’: (SyntaxError: invalid syntax)
I came across this post: Error exporting HTML files, in which it was suggested to use $‘Version’ + condition + ‘_selectBlock_conditioning.xlsx’ (as I used in the experiment before).

I am unfortunately still stuck with the errors as reported in my previous message (also when using the shuffle function as you suggested). Do you have any other ideas what could work?
The traceback shows that the variable condition is not defined, and that’s correct, as it is defined at the ‘beginning of the experiment’. Is this perhaps the issue?

Many thanks again!!!


I forked your project. However, the version you uploaded does not run offline. So, I can’t get to run online. Anyway, I notice that the program throws some warning with regard to deprecated names. It is itemText, itemWidth, itemColor instead of responseText aso. Changing this and inserting the equivalent python-code at to randomly select a condition the beginning of the experiment:

chars = ["A","B","C","D","E","F","G","H"]
condition = chars[0]

Unfortunately, the program throws an error because of an

Unknown colorSpace: named

But give the large number of routines and csv you are using I was unable to locate this.

Do you have a version, *.psyexp, that runs offline?

BTW. A lot of information that you are asking via routines, e.g. age, gender aso. can be easily accessed by extending the Experiment info. So you could cut down on your routines. :wink:

Best Jens


please find attached a version that was working for me off- and online. You might have to start it several times to see that it selects different stimulus files.

Demo.psyexp (7.9 KB) StimuliA.xlsx (8.5 KB) StimuliB.xlsx (8.5 KB)

Sorry, going back through my posting I notice that I was not clear enough in my descriptions, not making clear what is python-code, what is JavaScript. In addition, the following code has an error:

It has to be

characters = ["A","B","C","D","E","F","G"];
condition = characters[0];

Best Jens

Hi Jens,

Thanks so much for your advice and for sending the example experiment!

The example experiment works great offline and online. At a first try the demo reported the same error as with my experiment (name ‘cond’ not defined). After deleting an unrelated excel-file that happened to be in that same folder as your demo, the experiment synced.

I piloted my own experiment offline in several subjects, and it works fine so far. I uploaded the experiment I used for the pilot (as well as a newer version).

I followed your advice and suggestions with respect to the JS-code and Python-code, as well as the headings of the questionnaire-csv files. In these versions I also don’t get the error with respect to colorSpace.

I only kept the files I need for my experiment in my folder, but the issue is still there. I hope you still have some ideas on what would be helpful here, and why this specific error keeps popping up. I wonder whether the nested loops are the issue here.

Many thanks again!


Psychopy_Dec_update.psyexp (298.9 KB)
Psychopy_Oct29_update.psyexp (262.9 KB)


what is the issue? The error related to colorSpace or the error related to cond not being defined?

Best Jens

Hi Jens,

My apologies for not being clear. I am referring to the error related to cond not being defined.

I found out that the ‘cond not defined’-error occured because there were some ~$ files still linked to my folder. I deleted these files and it seems that no other errors are being reported and a .js-file is being created.