Aleks G
Aleks G

Reputation: 57316

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?

Upvotes: 3

Views: 1851

Answers (2)

bjareczek
bjareczek

Reputation: 1

I updated to latest stable version of node (nvm use 16 in my case).

This alone did not work, at least in my case. I could see that my script kept running under node v8.

On mac, use Go To Folder in finder (in my case): /Users/{me}/.nvm/versions/node/v8.12.0

Delete bin.

Ran my script again without any extra require statements and works as expected.

Upvotes: 0

Aleks G
Aleks G

Reputation: 57316

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.

Upvotes: 2

Related Questions