3

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?

7

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@JerryLiu Please do not make significant changes to existing accepted answer. If it's a comment, post a comment. If it's something significantly different, post it as your own answer
Sorry for my reckless, I thought it was just a complement as you have found the key point that causes the trouble, will add a comment instead.
If we set WEBSITE_NODE_DEFAULT_VERSION(In Application settings) to 10.14.1(latest in Azure), we don't have to import url module. Simply use let myUrl = new URL(someInputParameter);
0

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.

2 Comments

What does Mac have to do with Azure function? As I wrote, everything worked perfectly fine on local.
Good question. Replies are not only meant for the original poster, right? In case someone else is reading this thread... which I think is safe to assume... they might just need to take the same extra step that I did - on my mac (which is obviously local as well)

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.