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

JavascriptRuby on-RailsGitDeploymentExecjs

Javascript Problem Overview


When deploying my Rails app I get the following error:

rake aborted!
   ExecJS::ProgramError: Unexpected token punc «(», expected punc «:» (line: 15, col: 14, pos: 265)
   
   Error
   at new JS_Parse_Error (/tmp/execjs20150524-4411-1p45n63js:2359:10623)
   at js_error (/tmp/execjs20150524-4411-1p45n63js:2359:10842)
   at croak (/tmp/execjs20150524-4411-1p45n63js:2359:19086)
   at token_error (/tmp/execjs20150524-4411-1p45n63js:2359:19223)
   at expect_token (/tmp/execjs20150524-4411-1p45n63js:2359:19446)
   at expect (/tmp/execjs20150524-4411-1p45n63js:2359:19584)
   at /tmp/execjs20150524-4411-1p45n63js:2359:28513
   at /tmp/execjs20150524-4411-1p45n63js:2359:19957
   at expr_atom (/tmp/execjs20150524-4411-1p45n63js:2359:27269)
   at maybe_unary (/tmp/execjs20150524-4411-1p45n63js:2359:30019)new JS_Parse_Error ((execjs):2359:10623)
   js_error ((execjs):2359:10842)
   croak ((execjs):2359:19086)
   token_error ((execjs):2359:19223)
   expect_token ((execjs):2359:19446)
   expect ((execjs):2359:19584)
   (execjs):2359:28513
   (execjs):2359:19957
   expr_atom ((execjs):2359:27269)
   maybe_unary ((execjs):2359:30019)

The file in question is valid, it works on localhost. I also tried running rake assests:precompile on localhost, it all passes. Finally, I tried to remove the content from the file, git push and redeploy - still got the same error. Only completely removing the file and re-deploying helps.

Would appreciate any ideas.

Javascript Solutions


Solution 1 - Javascript

Here I found help for the same problem you had.

Run rails console and:

JS_PATH = "app/assets/javascripts/**/*.js"; 
Dir[JS_PATH].each do |file_name|
  puts "\n#{file_name}"
  puts Uglifier.compile(File.read(file_name), harmony: true)
end

It will show you the file and the line where the Uglifier is making the problem.

Solution 2 - Javascript

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 "(".

Solution 3 - Javascript

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.

Solution 4 - Javascript

I'm not sure of your build chain, but I got here by pasting the same error message into Google.

That is called 'shorthand properties' in ES2015. I'm using Babel 6 with Gulp and needed to do an npm install babel-plugin-transform-es2015-shorthand-properties --save-dev and add that transform to my babel plugins.

.pipe(babel({
    plugins: [
        'transform-es2015-shorthand-properties'
    ]
}))

https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-es2015-shorthand-properties

Solution 5 - Javascript

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

Solution 6 - Javascript

If Radovan's answer isn't working for you due to a problem in a library instead of your code, you can try upgrading Uglifier and enabling ES6 compilation.

Gemfile.lock

gem 'uglifier', '~> 4.1'

config/environments/production.rb

config.assets.js_compressor = Uglifier.new(harmony: true)

Solution 7 - Javascript

December/2019 answer: starting on version 4.2.0 (released in Sept/2019), Uglifier now shows a beautiful (colored!) debug output showing you the offending line of code.

I was having a Uglifier::Error: Unexpected character ''` error and I couldn't find it even following all the other solutions in this page.

So go to your Gemfile, and set your Uglifier to be at least 4.2:

# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 4.2'

Run bundle update uglifier to update it.

And then just look at the output, it will show you something like this:

enter image description here

Solution 8 - Javascript

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 https://stackoverflow.com/questions/894860/set-a-default-parameter-value-for-a-javascript-function

Solution 9 - Javascript

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.

Solution 10 - Javascript

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

config.assets.js_compressor = Uglifier.new(harmony: true)

Solution 11 - Javascript

If you are maintaining a legacy project(with ruby very old version e.g. 1.9.3 and Rails 3.2.x), I suggest you don't use uglifier&exec-js, simply comment out this line of code from config/environments/production.rb:

config.assets.compress = true

also make sure you installed nodejs as the js run time.

refer to: https://stackoverflow.com/questions/12574977/rake-assetsprecompile-gives-punc-error

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsnitkoView Question on Stackoverflow
Solution 1 - JavascriptRadovan SkendzicView Answer on Stackoverflow
Solution 2 - JavascriptMadCoderView Answer on Stackoverflow
Solution 3 - Javascript湯凱甯View Answer on Stackoverflow
Solution 4 - Javascriptjamie-wilsonView Answer on Stackoverflow
Solution 5 - JavascriptAmrudeshView Answer on Stackoverflow
Solution 6 - JavascriptsmaView Answer on Stackoverflow
Solution 7 - Javascriptsandre89View Answer on Stackoverflow
Solution 8 - JavascriptShilpeshAgreView Answer on Stackoverflow
Solution 9 - JavascriptArnold RoaView Answer on Stackoverflow
Solution 10 - JavascriptAbelView Answer on Stackoverflow
Solution 11 - JavascriptSiweiView Answer on Stackoverflow