Generating html files: Unexpected token

Hello PsychoPy community;

I am working on moving an experiment from my local machine to Pavlovia.
When I try to export to html, I get this error message:

371.9842     ERROR     Line 120: Unexpected token in meloChoice_go.js
Alert 4210:JavaScript Syntax Error in 'Begin JS Experiment' tab. See 'Line 12: Unexpected token' in the 'Begin JS Experiment' tab.
	For further info see https://psychopy.org/alerts/4210.html
Alert 4210:JavaScript Syntax Error in 'Begin JS Experiment' tab. See 'Line 12: Unexpected token' in the 'Begin JS Experiment' tab.
	For further info see https://psychopy.org/alerts/4210.html
372.8657     ERROR     Line 112: Unexpected token in meloChoice_go-legacy-browsers.js

I checked the .js file, and this seems to be something in the experiment initiation. I made the experiment in builder so I couldn’t have touched that code. Any ideas?

1 Like

@aisa2, the alert message describes that there is an error in one of your code components, in the “Begin Experiment” tab. Do you have any code in those tabs which may be causing the error?

@dvbridges thanks for your help; I don’t have a tab named “Begin Experiment” or “Begin JS Experiment.”

It looks like the error is in the .js file, not the python script; when I looked up the lines it references, this is the block of code it’s referring to:

function experimentInit() {
  // Initialize components for Routine "expSetup"
  expSetupClock = new util.Clock();
  
          // add-on: list(s: string): string[]
          function list(s) {
              // if s is a string, we return a list of its characters
              if (typeof s === 'string')
                  return s.split('');
              else
                  // otherwise we return s:
                  return s;
          }

I should note that I copied this psyexp file from somewhere else, and that I was able to successfully convert the original to html…

I don’t think that code snippet contains the error. If you can open the psyexp file in Builder, you will have a code component somewhere in a routine, and within that code component you will have a “begin experiment” tab, which will contain the error.

Ah, sorry, I thought you meant a routine called “Begin Experiment” and not a tab within a code element.
I’ve gone through the experiment again, and I found two places where the translation to JS seems a little strange. One of them is, indeed, in the Begin Experiment tab. The python side looks like:

# import libraries (random & np already imported natively)
import numpy as np
import random
import pandas as pd
from scipy.stats import norm

… and the JS side looks like:


        // add-on: list(s: string): string[]
        function list(s) {
            // if s is a string, we return a list of its characters
            if (typeof s === 'string')
                return s.split('');
            else
                // otherwise we return s:
                return s;
        }
        
        import * as np from 'numpy';
import * as random from 'random';
import * as pd from 'pandas';
import {norm} from 'scipy/stats';

So, it looks like a broken function was inserted above my code. The other place I found this happening was in a “Begin Routine” tab, and was largely similar.
Python:


# record block iteration
thisExp.addData('iblock', iblock)

# get list number & pitch condition
thisList = listOrder[iblock] +1 # get list presented
pitchCond = listConds[iblock] # get pitch condition
thisExp.addData('listNum', thisList)
thisExp.addData('pitchCond', pitchCond)

And JS:


        // add-on: list(s: string): string[]
        function list(s) {
            // if s is a string, we return a list of its characters
            if (typeof s === 'string')
                return s.split('');
            else
                // otherwise we return s:
                return s;
        }
        
        thisExp.addData("iblock", iblock);
thisList = (listOrder[iblock] + 1);
pitchCond = listConds[iblock];
thisExp.addData("listNum", thisList);
thisExp.addData("pitchCond", pitchCond);

I’m not sure how to change this (I’ve played around with cutting out lines of code but it does nothing). Is this something you’ve seen before?

1 Like

You will need to remove the import statements, these are Python library imports that will not work online. Take a look at Wakefields crib sheet for more pointers.

1 Like

Thanks, I just found that on another thread! Very useful sheet.

Removing the import statements did help; now I’ll just have to find an alternative way to read in my excel files and do math functions. There is one more error still showing up, but it is probably unrelated, so I’ll go ahead and mark this as solved.