uglifier icon indicating copy to clipboard operation
uglifier copied to clipboard

NoMethodError: undefined method `start_with?' for nil:NilClass

Open light-flight opened this issue 6 years ago • 13 comments

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

light-flight avatar Jun 08 '18 16:06 light-flight

Using the options bellow did the tricky to me. No idea why though ¯\(°_o)/¯

Uglifier.new(harmony: true, compress: { unused: false })

phlcastro avatar Aug 03 '18 14:08 phlcastro

@phlcastro my savior you are.

zackperdue avatar Aug 03 '18 16:08 zackperdue

@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)

benlinton avatar Aug 21 '18 16:08 benlinton

@phlcastro thx that worked

reicheltd avatar Sep 01 '18 07:09 reicheltd

@phlcastro That worked thanks! No idea what it's doing though.

choncou avatar Oct 11 '18 14:10 choncou

compress: { unused: false } did the trick! question is: did anyone understand why?

carlomartinucci avatar Jan 09 '19 16:01 carlomartinucci

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.

mattt avatar Jan 23 '19 18:01 mattt

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.

dineshpanda avatar Feb 27 '19 10:02 dineshpanda

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

cesc1989 avatar Mar 29 '19 01:03 cesc1989

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

gasi avatar Dec 16 '19 15:12 gasi

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

chinna2580 avatar Apr 15 '20 10:04 chinna2580

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.

jlurena avatar Jun 24 '20 01:06 jlurena

uglifier does NOT support ES6 completely, so I switched to "https://github.com/ahorek/terser-ruby".

https://github.com/lautis/uglifier/commit/902cc160d3f8f405a35a8f16da227489366a3ad6

yoshihiko-ikenaga avatar Jan 05 '21 00:01 yoshihiko-ikenaga