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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30422437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:09:52  来源:igfitidea点击:

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

javascriptruby-on-railsgitdeploymentexecjs

提问by snitko

When deploying my Rails app I get the following error:

部署 Rails 应用程序时,出现以下错误:

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:precompileon 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.

有问题的文件是有效的,它适用于本地主机。我也试过rake assests:precompile在本地主机上运行,一切都过去了。最后,我尝试从文件中删除内容, git push 并重新部署 - 仍然出现相同的错误。只有完全删除文件并重新部署才有帮助。

Would appreciate any ideas.

将不胜感激任何想法。

回答by Radovan Skendzic

Here I found help for the same problem you had.

在这里,我找到了解决您遇到的相同问题的帮助。

Run rails console and:

运行 rails 控制台并:

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.

它将向您显示 Uglifier 产生问题的文件和行。

回答by MadCoder

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

我怀疑,在那个 js 文件中,你有如下内容:

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

错误清楚地表明,它期待“:”但它找到了“(”。

回答by u5776746

Just encounter the same issue.

刚遇到同样的问题。

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

我的情况是有人使用了自 ES2015 以来仅支持的语法,例如

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

while this is not supported in our environment.

虽然这在我们的环境中不受支持。

And the error messages is actually generated by Uglifer.

而错误信息实际上是由 Uglifer 生成的。

回答by jamie-wilson

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

我不确定您的构建链,但我是通过将相同的错误消息粘贴到 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-devand add that transform to my babel plugins.

这在 ES2015 中称为“速记属性”。我正在将 Babel 6 与 Gulp 一起使用,需要做一个npm install babel-plugin-transform-es2015-shorthand-properties --save-dev并将该转换添加到我的 babel 插件中。

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

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

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

回答by Amrudesh

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

我可以使用https://skalman.github.io/UglifyJS-online/来确定问题所在的正确行号。值得庆幸的是,至少有问题的正确文件被 grunt uglify 指出

回答by ShilpeshAgre

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.

由于上述函数定义,我收到错误消息,因为 Uglifier 不支持它。默认参数是 ES6/ES2015 语言规范。

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

对于上述问题的解决方案,您可以参考为 JavaScript 函数设置默认参数值

回答by sma

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.

如果 Radovan 的答案由于库中的问题而不是您的代码而对您不起作用,您可以尝试升级 Uglifier 并启用 ES6 编译。

Gemfile.lock

Gemfile.lock

gem 'uglifier', '~> 4.1'

config/environments/production.rb

配置/环境/production.rb

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

回答by sandre89

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.

2019 年 12 月回答:从 4.2.0 版(2019 年 9 月发布)开始,Uglifier 现在显示漂亮的(彩色!)调试输出,向您显示有问题的代码行。

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

我遇到了一个Uglifier::Error: Unexpected character ''` 错误,即使按照此页面中的所有其他解决方案也找不到它。

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

因此,转到您的 Gemfile,并将您的 Uglifier 设置为至少 4.2:

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

Run bundle update uglifierto update it.

运行bundle update uglifier以更新它。

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

然后只需查看输出,它会显示如下内容:

enter image description here

在此处输入图片说明

回答by Abel

Update uglifiergem to last version and update your production.rb

uglifiergem更新到最新版本并更新您的 production.rb

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

回答by Arnold Roa

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

由于回溯不提供有关损坏文件的信息,对我来说,识别错误的最佳方法是使用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:

假设你在 master 上,首先你启动 git bisect:

$ git bisect start
$ git bisect bad 

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

然后你回到以前的工作修订版,假设是 20 年前的修订版。

$ 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.

git 将跳转到另一个修订版,您再次运行相同的命令 (assets:precompile) 并根据输出将其标记为好/坏。

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

在不到 1 分钟的时间内,您应该能够找到导致该问题的提交。