It should be added to an Editor script (Editor Scripting - Unity Learn). Try the attached file for example. Place the file in directory UnityProject/Assets/Editor/buildWebGL.cs, then edit the script to configure the “levelsToPack” variable. Then use one of the newly appeared HTML5 Export menu items to build.
Note the line 69 in that script that adds the -s ERROR_ON_UNDEFINED_SYMBOLS=0 directive.