I am trying to read a file using WHATWG URL object support here
and I am getting this error: Uncaught TypeError: URL is not a constructor
here is my code:
var fs = require("fs");
const { URL } = require('url');
var dbPath = 'file://192.168.5.2/db/db.sqlite';
const fileUrl = new URL(dbPath);
Are you using Node 6 instead of Node 8?
Node 6
https://nodejs.org/dist/latest-v6.x/docs/api/url.html#url_url
Node 8
https://nodejs.org/dist/latest-v8.x/docs/api/url.html#url_url
I faced the same issue, then I looked into the url module and found a solution
For Node V6 use,
or
If you look into the module, it exports 5 methods one of which is Url, so if you need to access Url, you can use either of the two methods
Node v10
URL Class
As mentioned here: https://nodejs.org/docs/latest-v10.x/api/url.html#url_class_url
So this should work without
require('url')
:The docs you took this info out are for the
node
of version8.4.0
.If it does not work for you, that means that your
node
is of version6.11.2
. Then, just change the letter case ofURL
-because
url
module exportsUrl
, notURL
.