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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With