17

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

3 Answers 3

47

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");
Sign up to request clarification or add additional context in comments.

9 Comments

If you're targeting Node.js do not use that import syntax. Use import m = require("m");
@AluanHaddad Any reasons?
Yes many. One very important reason is github.com/nodejs/node-eps/blob/master/…
Thanks a lot dude, it was totally a version error, I had googled url parsers and was directed to the newest docs which caused the confusion, unfortunately the 6.x.x node url module does not contain an origin property but I can easily concat the protocol with the hostname
Also @AluanHaddad thanks for the link about importing modules, I have been using typescript for my quick node scripts and it's good to know why I should keep it to node standards
|
3
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
>

1 Comment

If you're targeting Node.js do not use that import syntax. Use import m = require("m");
3

If someone is wondering how to use URL so code is portable between Node.js and browser environment, then i've came to this solution:

import * as nodeUrl from "url";

const URL = typeof window !== "undefined" ? window.URL : nodeUrl.URL;

1 Comment

had to add declare var window; for it to work as answered here stackoverflow.com/a/61871419/1997873

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.