Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL is not defined in node.js

Tags:

url

node.js

let url = new URL("https://stackoverflow.com/questions/ask") 

It will get an error like this:

URL is not defined

Actually, in modern browsers, it works well. However, if you run it in the node.js environment, you will get an error like that.

I researched the documentation, it says it's a global class:

Browser-compatible URL class, implemented by following the WHATWG URL Standard. Examples of parsed URLs may be found in the Standard itself. The URL class is also available on the global object.

So, what's the problem?

like image 864
zzzgoo Avatar asked Sep 29 '18 08:09

zzzgoo


People also ask

What is URL in node js?

The node:url module provides two APIs for working with URLs: a legacy API that is Node. js specific, and a newer API that implements the same WHATWG URL Standard used by web browsers. A comparison between the WHATWG and Legacy APIs is provided below.

What is CI CD in node js?

js with GitHub Actions. January 7, 2022 10 min read. Continuous integration/continuous deployment is a software engineering practice that helps teams to collaborate better and improve their overall software.


Video Answer


1 Answers

Try to add this line:

var URL = require('url').URL; 

The full example is here:

var URL = require('url').URL; var myURL = new URL('http://www.example.com/foo?bar=1#main');  console.log(myURL.host);  // prints 'www.example.com' 

All of those are from this amazing tutorial.

like image 165
You Nguyen Avatar answered Oct 07 '22 19:10

You Nguyen