Lighthouse: ReferenceError: URL is not defined

Created on 9 May 2019  ·  11Comments  ·  Source: GoogleChrome/lighthouse

Getting this error in my CI after updating to version 5.0.0, I think it has to do something with 94800e160b9510e31d3a7f459da81991e500fe9e

/usr/lib/node_modules/lighthouse/lighthouse-core/lib/url-shim.js:35
class URLShim extends URL {
                      ^

ReferenceError: URL is not defined
    at Object.<anonymous> (/usr/lib/node_modules/lighthouse/lighthouse-core/lib/url-shim.js:35:23)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/usr/lib/node_modules/lighthouse/lighthouse-core/lib/network-request.js:14:13)
    at Module._compile (module.js:652:30)
child_process.js:644
    throw err;

Provide the steps to reproduce

lighthouse --chrome-flags="--disable-gpu --headless --no-sandbox" https://google.com --output json --output html --output-path ./.lighthouse/google.com_05-09-19-1557389996769.json

Environment Information

  • Affected Channels: CLI
  • Lighthouse version: 5.0.0
  • Node.js version: 8.11.1
  • Operating System: Ubuntu 17.10

Most helpful comment

I was facing the same problem but I found out that the latest version of this package requires Node 10 or above.

Relevant links:
Breaking Changes - https://github.com/GoogleChrome/lighthouse/releases/tag/v5.0.0
PR - https://github.com/googlechrome/lighthouse/pull/8117

All 11 comments

I was facing the same problem but I found out that the latest version of this package requires Node 10 or above.

Relevant links:
Breaking Changes - https://github.com/GoogleChrome/lighthouse/releases/tag/v5.0.0
PR - https://github.com/googlechrome/lighthouse/pull/8117

@cristianoliveira thanks, I didn't notice that

@evromalarkey You can try the previous version of lighthouse as ->

npm install -g [email protected] --save-exact
or
switch to a newer node version >=10.0

Updating / Switching to any node version above 10.0 solves the issue.

Add this line at the top of script solved my issue
global.URL = require('url').URL;

@bitrecipe while that might work right now, the only officially supposed node version moving forward is v10 LTS, so just be aware that future Lighthouse versions might end up breaking you unexpectedly if you're still on v8.

@patrickhulce I know this is a shortcut method not the correct approach but it's works on Node.js version 8, if he still need Node.js version 8 for some reason

I am getting the same error.

node --version
v12.18.3

I added the command in a bash script. When I run the bash script, everything works well.
When I run the script as a cron job, I get the error URL is not defined.
Any ideas?

When I run the script as a cron job, I get the error URL is not defined.

I'm guessing the node version used by the cron job is falling back to an older system version than the one used by you in a bash script. Check which node and make sure they're using the same?

That is absolutely correct! From my terminal

 which node
/usr/local/bin/node

but from cron it's /usr/bin/node.

~$ /usr/bin/node --version
v8.10.0
~$ /usr/local/bin/node --version
v12.18.3

Any suggestions?

I did something which I don't know if it was a good idea. I don't know how I could have removed or upgraded the /usr/bin/node old version, so I did the following.

sudo mv /usr/bin/node /usr/bin/node.old
sudo ln -s /usr/local/bin/node /usr/bin/node

and my command worked from the cron job.

Was this page helpful?
0 / 5 - 0 ratings