Unable to import URL class in Nodejs Typescript app
import * as url from 'Url';
{ parse: [Function: urlParse],
resolve: [Function: urlResolve],
resolveObject: [Function: urlResolveObject],
format: [Function: urlFormat],
Url: [Function: Url]
}
It's not a constructor. You'll need to do use one of the exposed methods:
import * as url from 'url';
console.log(url.parse('https://www.google.com'))
// Output:
Url {
protocol: 'https:',
slashes: true,
auth: null,
host: 'www.google.com',
port: null,
hostname: 'www.google.com',
hash: null,
search: null,
query: null,
pathname: '/',
path: '/',
href: 'https://www.google.com/' }
undefined
>
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");