uglifier
uglifier copied to clipboard
NoMethodError: undefined method `start_with?' for nil:NilClass
NoMethodError: undefined method `start_with?' for nil:NilClass
My setup:
ruby 2.4.2p198
rails-5.1.6
uglifier-4.1.11
therubyracer-0.12.3
Error raised during deployment. Recently I've added @ckeditor/ckeditor5-build-classic npm package which contains ES6 syntax.
# also I've changed
config.assets.js_compressor = :uglifier
# to
config.assets.js_compressor = Uglifier.new(harmony: true)
Now I'm getting this error. Here is capistrano log
Reproduction:
Uglifier.compile(File.read("node_modules/@ckeditor/ckeditor5-build-classic/build/ckeditor.js"), harmony: true)
> NoMethodError: undefined method `start_with?' for nil:NilClass
Using the options bellow did the tricky to me. No idea why though ¯\(°_o)/¯
Uglifier.new(harmony: true, compress: { unused: false })
@phlcastro my savior you are.
@phlcastro's solution worked for me as well
uglifier (4.1.18)
rails (5.2.1)
mini_racer (0.2.0)
ruby (2.5.1)
@phlcastro thx that worked
@phlcastro That worked thanks! No idea what it's doing though.
compress: { unused: false }
did the trick! question is: did anyone understand why?
I encountered this by way of jekyll-assets. All of the sudden, for no apparent reason, my Jekyll site stopped compiling. Instead, jekyll serve
generated this warning:
Liquid Exception: undefined method `start_with?' for nil:NilClass
Adding a --trace
option, I was able to track the problem to uglifier-4.1.20/lib/uglifier.rb:225
(third line below, cc @carlomartinucci):
def error_message(result)
result['error']['message'] +
if result['error']['message'].start_with?("Unexpected token") && !harmony?
". To use ES6 syntax, harmony mode must be enabled with " \
"Uglifier.new(:harmony => true)."
else
""
end
end
Based on this, my understanding is that my site stopped building successfully because of an error caused by Uglifier not knowing how to compress an unused declaration. Fun.
The fix for me (again, in Jekyll) was to define the following configuration (thanks to the tip from @phlcastro):
assets:
compressors:
uglifier:
harmony: true
compress:
unused: false
Unfortunately, this is undocumented. I only knew to try this thanks to this commit message.
I'm posting this for the benefit of any other jekyll-assets users who encounter this error and are looking for a resolution.
I am facing the same issue and I have the following gems:
uglifier (4.1.9) rails (5.2.2) therubyracer (0.12.3)
and server has node version v6.16.0 .
I also added config.assets.js_compressor = Uglifier.new(harmony: true, compress: { unused: false })
to config/environments/production.rb
.
But in my case the above did not resolve the issue and not sure how to fix this issue.
Kindly let me know what could be causing the issue while precompiling the assets in production.
I also was benefited by the most accepted comment :D
Uglifier.new(harmony: true, compress: { unused: false })
My gem versions:
rails (5.2.1)
uglifier (4.1.18)
ruby 2.5.3
FWIW, this is fixed in latest Uglifier 4.2.0 Gem:
- https://github.com/lautis/uglifier/blob/77226d194e6eae5e82df53452c92a7b3727d28b9/lib/uglifier.rb#L228-L235
- https://github.com/lautis/uglifier/blob/77226d194e6eae5e82df53452c92a7b3727d28b9/lib/uglifier.rb#L283-L288
it's because, if your javascript contains latest ES8 syntax like let { name: newName } = info; atleast in my case, probably the gem supports only ES6 syntax
FWIW, this is fixed in latest Uglifier 4.2.0 Gem:
- https://github.com/lautis/uglifier/blob/77226d194e6eae5e82df53452c92a7b3727d28b9/lib/uglifier.rb#L228-L235
- https://github.com/lautis/uglifier/blob/77226d194e6eae5e82df53452c92a7b3727d28b9/lib/uglifier.rb#L283-L288
4.2.0 did not fix the issue.
uglifier does NOT support ES6 completely, so I switched to "https://github.com/ahorek/terser-ruby".
https://github.com/lautis/uglifier/commit/902cc160d3f8f405a35a8f16da227489366a3ad6