Search code examples
javascriptruby-on-railsrubyprecompileexecjs

ExecJS::ProgramError: SyntaxError: Unexpected token: name <ClassName>


I've created a JavaScript class (named as BarChart) that have constructor and several methods init.

I executed below command to pre-compile assets for production environment but got error.

Command used to pre-compiling assets: rake assets:precompile RAILS_GROUPS=assets --trace

Error backtrace:

rake aborted!
ExecJS::ProgramError: SyntaxError: Unexpected token: name (BarChart) (line: 24472, col: 6, pos: 952834)

Error
    at new JS_Parse_Error (<eval>:3623:11948)
    at js_error (<eval>:3623:12167)
    at croak (<eval>:3623:22038)
    at token_error (<eval>:3623:22175)
    at unexpected (<eval>:3623:22263)
    at semicolon (<eval>:3623:22781)
    at simple_statement (<eval>:3623:25959)
    at <eval>:3623:23747
    at <eval>:3623:22954
    at <eval>:3624:3759
    at Object.parse (<eval>:3624:3999)
    at parse (<eval>:3958:22)

Javascript class which throws error is written like:

class BarChart
{
    // code goes here
}

I've added therubyracer and execjs gem in Gemfile.

Ruby: 2.2.5

Rails: 3.2.2`

PS: I've checked several questions of this type in SO, but none of the issue is related to JavaScript class.


Solution

  • I'd written the JavaScript in ES6 and rails 3 shows error while ES6 code. All the methods of using ES6 in rails is not for rails 3. So I converted ES6 to ES5 code.