Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to import URL class in Nodejs Typescript app

I am trying to import the URL class from the nodejs api so I can parse urls and retrieve their origin, hostname, and paths.

I am trying to do it by typing import * as URL from "url";

and I have also tried import { URL } from "url";

and I cannot seem to get this to work for me. Any help would be greatly appreciated.

The way I am trying to use this is as such

var site = new URL("http://www.website.com");

And it is throwing an error because it states that URL is not a constructor

like image 434
BossmanT Avatar asked Sep 26 '17 01:09

BossmanT


1 Answers

The URL constructor was introduced as part of node v7.0. import { URL } from "url"; is the proper way to import it if you are using node >= v7.0. Make sure you are using the latest version of node typings as updated as well:

npm install --save-dev @types/node

If your node version is < 7.0 then you can use the parse method:

import * as url from "url";

var site = url.parse("http://www.website.com");
like image 175
Saravana Avatar answered Sep 21 '22 10:09

Saravana