Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
light-flight opened this issue Jun 8, 2018 · 13 comments
Open

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

light-flight opened this issue Jun 8, 2018 · 13 comments

Comments

@light-flight
Copy link

light-flight commented Jun 8, 2018

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
@phlcastro
Copy link

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

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

@zackperdue
Copy link

@phlcastro my savior you are.

@benlinton
Copy link

benlinton commented Aug 21, 2018

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

@reicheltd
Copy link

@phlcastro thx that worked

@choncou
Copy link

choncou commented Oct 11, 2018

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

@carlomartinucci
Copy link

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

@mattt
Copy link

mattt commented Jan 23, 2019

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.

@dineshpanda
Copy link

dineshpanda commented Feb 27, 2019

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.

@cesc1989
Copy link

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

@gasi
Copy link

gasi commented Dec 16, 2019

FWIW, this is fixed in latest Uglifier 4.2.0 Gem:

  • uglifier/lib/uglifier.rb

    Lines 228 to 235 in 77226d1

    def harmony_error_message(message)
    if message.start_with?("Unexpected token")
    ". To use ES6 syntax, harmony mode must be enabled with " \
    "Uglifier.new(:harmony => true)."
    else
    ""
    end
    end
  • uglifier/lib/uglifier.rb

    Lines 283 to 288 in 77226d1

    def error_message(result, options)
    err = result['error']
    harmony_msg = harmony? ? '' : harmony_error_message(err['message'].to_s)
    src_ctx = context_lines_message(options[:source], err['line'], err['col'])
    "#{err['message']}#{harmony_msg}\n#{src_ctx}"
    end

@chinna2580
Copy link

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

@jlurena
Copy link

jlurena commented Jun 24, 2020

FWIW, this is fixed in latest Uglifier 4.2.0 Gem:

  • uglifier/lib/uglifier.rb

    Lines 228 to 235 in 77226d1

    def harmony_error_message(message)
    if message.start_with?("Unexpected token")
    ". To use ES6 syntax, harmony mode must be enabled with " \
    "Uglifier.new(:harmony => true)."
    else
    ""
    end
    end
  • uglifier/lib/uglifier.rb

    Lines 283 to 288 in 77226d1

    def error_message(result, options)
    err = result['error']
    harmony_msg = harmony? ? '' : harmony_error_message(err['message'].to_s)
    src_ctx = context_lines_message(options[:source], err['line'], err['col'])
    "#{err['message']}#{harmony_msg}\n#{src_ctx}"
    end

4.2.0 did not fix the issue.

@yoshihiko-ikenaga
Copy link

yoshihiko-ikenaga commented Jan 5, 2021

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

902cc16

kueda added a commit to inaturalist/inaturalist that referenced this issue May 11, 2021
As of c8b403b, some of our dependencies are in
ES6 or higher. Previously, we had configured webpack to ignore everything in
node_modules during it's Babel step because everything we were using was already
transformed into more browser-compliant forms of JS, but since we can no longer
assume that is true, we need to run everything through Babel so if a dependency
uses ES6, we perform our own transformations to browser-compliant JS. This slows
things down and seems like it might increase the size of the JS assets. If we
don't do this, we end up running a JS file with ES6 syntax through Uglifier
during asset precompilation, which gives it indigestion. Uglifier *can* be
configured to handle ES6 with `harmony: true`, but trying that led to other
problems with the version of Uglifier we're using that may not have been
resolved (see lautis/uglifier#137).

Note that I tried experimenting with the `exclude` feature in the webpack config
so it wouldn't exclude certain directories in node_modules, but it's really hard
to figure out exactly what dependency is using some piece of ES6 syntax that
causes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests