Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReferenceError: URL is not defined in Azure javascript function

I'm using Azure Function App to handle simple API calls. I'm using JavaScript as the language. I developed locally and tested with func host start and confirmed everything working as needed. Part of the code is parsing a URL. I have the following in my function code:

const url = require('url');
let myUrl = new URL(someInputParameter);

As indicated, this works fine when tested locally, however when deployed in azure, I receive this error message:

018-12-11T10:40:56.236 [Error] Executed 'Functions.myFunction' (Failed, Id=d7d51ed1-d37e-44ec-91d0-070de2005c1c)
Result: Failure
Exception: ReferenceError: URL is not defined
Stack: ReferenceError: URL is not defined
    at module.exports (D:\home\site\wwwroot\myFunction\index.js:8:15)
    at WorkerChannel.invocationRequest (D:\Program Files (x86)\SiteExtensions\Functions\2.0.12210\32bit\workers\node\worker-bundle.js:28862:26)
    at ClientDuplexStream.WorkerChannel.eventStream.on (D:\Program Files (x86)\SiteExtensions\Functions\2.0.12210\32bit\workers\node\worker-bundle.js:28752:30)
    at emitOne (events.js:116:13)
    at ClientDuplexStream.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
    at ClientDuplexStream.Readable.push (_stream_readable.js:208:10)
    at Object.onReceiveMessage (D:\Program Files (x86)\SiteExtensions\Functions\2.0.12210\32bit\workers\node\worker-bundle.js:42351:19)
    at InterceptingListener.module.exports.InterceptingListener.recvMessageWithContext (D:\Program Files (x86)\SiteExtensions\Functions\2.0.12210\32bit\workers\node\worker-bundle.js:41678:19)

Do note that I do require('url'). How do I resolve this?

like image 217
Aleks G Avatar asked Mar 06 '26 15:03

Aleks G


1 Answers

I figured it out eventually. The code runs in Node 8, therefore require line needs to look like this:

const { URL } = require('url');

Now everything works as expected.

like image 74
Aleks G Avatar answered Mar 08 '26 03:03

Aleks G