简体   繁体   English

ExecJS::ProgramError: 运行 rake 资产时出现意外的标记 punc «(»,预期的 punc «:»:在生产中预编译

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

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:precompile on localhost, it all passes.我还尝试在本地主机上运行rake assests:precompile ,一切都通过了。 Finally, I tried to remove the content from the file, git push and redeploy - still got the same error.最后,我尝试从文件中删除内容, git push 并重新部署 - 仍然出现相同的错误。 Only completely removing the file and re-deploying helps.只有完全删除文件并重新部署才有帮助。

Would appreciate any ideas.将不胜感激任何想法。

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 产生问题的文件和行。

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 "(".错误清楚地表明,它期待“:”但它找到了“(”。

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 生成的。

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.这在 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.我正在将 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

I could use https://skalman.github.io/UglifyJS-online/ to identify the correct line number where the issue was.我可以使用https://skalman.github.io/UglifyJS-online/来确定问题所在的正确行号。 Thankfully, at least the correct file which had an issue was pointed out by grunt uglify值得庆幸的是,至少有问题的正确文件被 grunt uglify 指出

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)

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 uglifier to update it.运行bundle update uglifier来更新它。

And then just look at the output, it will show you something like this:然后只需查看输出,它会显示如下内容:

在此处输入图片说明

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.由于上述函数定义,我收到错误消息,因为 Uglifier 不支持它。 Default parameters is ES6/ES2015 language specification.默认参数是 ES6/ES2015 语言规范。

For solution to above problem you can refer Set a default parameter value for a JavaScript function对于上述问题的解决方案,您可以参考为 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.由于回溯不提供有关损坏文件的信息,对我来说,识别错误的最佳方法是使用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 分钟的时间内,您应该能够找到导致该问题的提交。

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

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

If you are maintaining a legacy project(with ruby very old version eg 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 :如果您正在维护一个遗留项目(使用 ruby​​ 非常旧的版本,例如 1.9.3 和 Rails 3.2.x),我建议您不要使用 uglifier&exec-js,只需从config/environments/production.rb注释掉这行代码:

config.assets.compress = true

also make sure you installed nodejs as the js run time.还要确保您安装了nodejs作为 js 运行时。

refer to: "rake assets:precompile" gives punc error参考: “rake assets:precompile”给出punc错误

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ExecJS :: ProgramError:尝试为生产预编译资产时出现意外字符'#' - ExecJS::ProgramError: Unexpected character '#' when trying to precompile assets for production 意外的标记 punc «(»,从 UglifyJS 创建块时预期的 punc - Unexpected token punc «(», expected punc when creating chunk from UglifyJS 意外的令牌运算符«=»,预期的punc«,» - Unexpected token operator «=», expected punc «,» 错误:在最小化angularjs应用程序时出现了意外的标记punc«)»,预期的punc«,» - Error: Unexpected token punc «)», expected punc «,» while minifying angularjs app SyntaxError:意外的标记:punc()) - SyntaxError: Unexpected token: punc ()) ExecJS :: RuntimeError:SyntaxError:意外的令牌:get @(execjs)中的punc()):3538:630 - ExecJS::RuntimeError: SyntaxError: Unexpected token: punc ()) from get@(execjs):3538:630 Uglify SyntaxError:意外的标记:punc ()) - Uglify SyntaxError: Unexpected token: punc ()) 意外标记:名称«Dom7»,预期:punc «;» 何时从 UglifyJs 创建块? - Unexpected token: name «Dom7», expected: punc «;» when create chunk from UglifyJs? git:ExecJS :: ProgramError:意外的令牌:推送到生产环境时的名称(jquery) - git: ExecJS::ProgramError: Unexpected token: name (jquery) when push to production environment 由于 Uglifier Punc 错误,无法预编译 Rails 资产 - Can't Precompile Rails Assets Due to Uglifier Punc Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM