[英]ExecJS::ProgramError: Unexpected character '#' when trying to precompile assets for production
[英]ExecJS::ProgramError: Unexpected token punc «(», expected punc «:» when running rake assets:precompile on production
部署 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)
有問題的文件是有效的,它適用於本地主機。 我還嘗試在本地主機上運行rake assests:precompile
,一切都通過了。 最后,我嘗試從文件中刪除內容, git push 並重新部署 - 仍然出現相同的錯誤。 只有完全刪除文件並重新部署才有幫助。
將不勝感激任何想法。
運行 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
它將向您顯示 Uglifier 產生問題的文件和行。
我懷疑,在那個 js 文件中,你有如下內容:
var User = {
getName() {
alert("my name");
}
}
用正確的格式替換它,
var User = {
getName: function() {
alert("my name");
}
}
為我工作。
錯誤清楚地表明,它期待“:”但它找到了“(”。
剛遇到同樣的問題。
我的情況是有人使用了自 ES2015 以來僅支持的語法,例如
function someThing(param = true) {
// do something here
};
雖然這在我們的環境中不受支持。
而錯誤信息實際上是由 Uglifer 生成的。
我不確定您的構建鏈,但我是通過將相同的錯誤消息粘貼到 Google 來實現的。
這在 ES2015 中稱為“速記屬性”。 我正在將 Babel 6 與 Gulp 一起使用,並且需要執行npm install babel-plugin-transform-es2015-shorthand-properties --save-dev
並將該轉換添加到我的 babel 插件中。
.pipe(babel({
plugins: [
'transform-es2015-shorthand-properties'
]
}))
我可以使用https://skalman.github.io/UglifyJS-online/來確定問題所在的正確行號。 值得慶幸的是,至少有問題的正確文件被 grunt uglify 指出
如果 Radovan 的答案由於庫中的問題而不是您的代碼而對您不起作用,您可以嘗試升級 Uglifier 並啟用 ES6 編譯。
Gemfile.lock
gem 'uglifier', '~> 4.1'
配置/環境/production.rb
config.assets.js_compressor = Uglifier.new(harmony: true)
2019 年 12 月回答:從 4.2.0 版(2019 年 9 月發布)開始,Uglifier 現在顯示漂亮的(彩色!)調試輸出,向您顯示有問題的代碼行。
我遇到了Uglifier::Error: Unexpected character '
'` 錯誤,即使按照此頁面中的所有其他解決方案我也找不到它。
因此,轉到您的 Gemfile,並將您的 Uglifier 設置為至少 4.2:
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 4.2'
運行bundle update uglifier
來更新它。
然后只需查看輸出,它會顯示如下內容:
就我而言,函數定義有問題,例如,
function someFunctionName(param1, param2=defaultValue){
//code
}
由於上述函數定義,我收到錯誤消息,因為 Uglifier 不支持它。 默認參數是 ES6/ES2015 語言規范。
對於上述問題的解決方案,您可以參考為 JavaScript 函數設置默認參數值
由於回溯不提供有關損壞文件的信息,對我來說,識別錯誤的最佳方法是使用git bisect。
它允許您找到引入錯誤的提交。
假設你在 master 上,首先你啟動 git bisect:
$ git bisect start
$ git bisect bad
然后你回到以前的工作修訂版,假設是 20 年前的修訂版。
$ git checkout HEAD~20
你運行相同的命令
$ RAILS_ENV=production rake assets:precompile
如果它有效,您將修訂標記為良好:
$ git bisect good.
git 將跳轉到另一個修訂版,您再次運行相同的命令 (assets:precompile) 並根據輸出將其標記為好/壞。
在不到 1 分鍾的時間內,您應該能夠找到導致該問題的提交。
將uglifier gem 更新到最新版本並更新您的 production.rb
config.assets.js_compressor = Uglifier.new(harmony: true)
如果您正在維護一個遺留項目(使用 ruby 非常舊的版本,例如 1.9.3 和 Rails 3.2.x),我建議您不要使用 uglifier&exec-js,只需從config/environments/production.rb
注釋掉這行代碼:
config.assets.compress = true
還要確保您安裝了nodejs
作為 js 運行時。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.