ExecJS::ProgramError: Unexpected token punc «(», expected punc «:» when running rake assets:precompile on production

后端 未结 10 1352
小鲜肉
小鲜肉 2020-11-28 20:08

When deploying my Rails app I get the following error:

rake aborted!
   ExecJS::ProgramError: Unexpected token punc «(», expected punc «:» (line: 15, col: 14         


        
相关标签:
10条回答
  • 2020-11-28 20:33

    I suspect, in that js file, you have something like the following:

    var User = {
        getName() {
            alert("my name");
        }
    }
    

    Replacing it with the right format,

    var User = {
        getName: function() {
            alert("my name");
        }
    }
    

    worked for me.

    Error is clearly saying, it's expecting ":" but it found "(".

    0 讨论(0)
  • 2020-11-28 20:33

    Just encounter the same issue.

    My case is someone used syntax that's only support since ES2015, ex

    function someThing(param = true) {
        // do something here
    };
    

    while this is not supported in our environment.

    And the error messages is actually generated by Uglifer.

    0 讨论(0)
  • 2020-11-28 20:36

    As the backtrace doesn't provide information about the corrupted file, for me the best way to identify the error is use git bisect.

    It allows you to find the commit that introduces a bug.

    Let's suppose you are on master, first you start git bisect:

    $ git bisect start
    $ git bisect bad 
    

    Then you go back to a previous, working revision, let's suppose 20 revision ago.

    $ git checkout HEAD~20
    

    You run the same command

    $ RAILS_ENV=production rake assets:precompile
    

    If it works you mark revision as good:

    $ git bisect good.
    

    git will jump to another revision, you run same command again (assets:precompile) and bassed on the output mark it as good / bad.

    In less than 1 minute you should be able to find what's the commit that introduced the issue.

    0 讨论(0)
  • 2020-11-28 20:38

    I could use https://skalman.github.io/UglifyJS-online/ to identify the correct line number where the issue was. Thankfully, at least the correct file which had an issue was pointed out by grunt uglify

    0 讨论(0)
  • 2020-11-28 20:41

    In my case problem with function definition like,

    function someFunctionName(param1, param2=defaultValue){
      //code 
    }
    

    Due to above function definition I was getting error, as it is not supported by Uglifier. Default parameters is ES6/ES2015 language specification.

    For solution to above problem you can refer Set a default parameter value for a JavaScript function

    0 讨论(0)
  • 2020-11-28 20:52

    Update uglifier gem to last version and update your production.rb

    config.assets.js_compressor = Uglifier.new(harmony: true)
    
    0 讨论(0)
提交回复
热议问题