ReferenceError: URL is not defined in Azure javascript function - javascript

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?

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.

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.

Related

Is it possible to spawn a local server in browserified web application

I'm currently try to run my electron application into a browser.
To do so, I'm using browserify.
The main issue is that I use a proxy to treat some data between my video stream client and my ipfs endpoints.
The proxy is made with the node module express but it appears that you can't use it in a browser.
I've checked for some alternative modules, but the problem is apparently not the module but the fact to spawn a server in the browser itself.
Here is the last reference I found that confirms the issue:
Here is the error I get from the browser:
login-bundle.js:73697 Uncaught TypeError: Cannot read property 'prototype' of undefined
at Object.<anonymous> (login-bundle.js:73697)
at Object.261../utils (login-bundle.js:74799)
at o (login-bundle.js:1)
at login-bundle.js:1
at Object.257../application (login-bundle.js:72936)
at o (login-bundle.js:1)
at login-bundle.js:1
at Object.255../lib/express (login-bundle.js:72264)
at o (login-bundle.js:1)
at login-bundle.js:1
refering to the following code:
var res = Object.create(http.ServerResponse.prototype)
where ServerResponse is undefined
That is why I found out that it was incompatible with the browser
I would like to know if anyone knows any solution to spawn a server into my browserified application.

How to get search domain of local Chromebook and NS records of that domain for Chrome app?

I would like to get the search domain from chromeOS notebook and then NS records of the search domain using javascript for chrome app. The chromeapp will be running on chromeOS.
I am able to get the records using resolveNs method of NodeJs "dns" library but it needs nodeJS. And for that I need to package nodeJS with chrome App? I am new to chrome os.
I tried to browserify that but got several issues. Is there any way to get the search domain and all NS records of a domain using JavaScript?
Or is there a way to browserify the 'dns' module of NodeJS. In my try, browserify does the job but when I open the javascript in chrome, it says: fs.readFile is not a function:
Uncaught TypeError: fs.readFile is not a function
at Platform.parseResolv (bundle.js:4295)
at Platform._populate (bundle.js:4248)
at new Platform (bundle.js:4201)
at Object.<anonymous> (bundle.js:4409)
at Object.require.18../utils (bundle.js:4411)
at s (bundle.js:1)
at bundle.js:1
at Object.<anonymous> (bundle.js:3155)
at Object.require.15../packet (bundle.js:3815)
at s (bundle.js:1)
Same happens when I replace fs by browserify-fs. My code in nodeJS is (it works fine when I say node filename.js):
var dnsresolve = require('native-dns/lib/client').resolve;
dnsresolve('xxx.pb1.lan', 'NS', function (err, addresses) {
if (err) throw err;
console.log('addresses: ' + JSON.stringify(addresses));
});

Nodegit and Electron not working together

I am getting this error with nodegit, and I am not sure if it is my fault for not knowing how to work with electron remote or it is a problem with nodegit. I did file an issue just in case.
In my main.js file for electron I can use nodegit without any issue.
On the web site I try to use nodegit with remote. I get an error. My code:
var remote = require('electron').remote;
var pathToRepo = remote.require('path').resolve(currentRepoDir);
remote.require('nodegit').Repository.open(pathToRepo).then(function(repo){
console.log(repo);
});
I am getting the error:
Uncaught TypeError: require(...).remote.require(...).Repository.open is not a function
Why is open not a function?
Why are you using remote.require?
remote.require('path')
Just use:
require('path')
require works in the render process too.

Node Express 500 Error: Unknown encoding

I am running an Express application on Nodejitsu. It's Node 0.8 and Express 3.4.0. The application works absolutely fine locally, but one of the routes has stopped working and returns the following error when I push to Nodejitsu:
Express
500 Error: Unknown encoding
at Buffer.toString (buffer.js:440:13)
at Object.fs.readFileSync (fs.js:241:33)
at handleCache (/opt/run/snapshot/package/node_modules/ejs/lib/ejs.js:65:21)
at exports.renderFile (/opt/run/snapshot/package/node_modules/ejs/lib/ejs.js:206:14)
at View.exports.__express [as engine] (/opt/run/snapshot/package/node_modules/ejs/lib/ejs.js:510:22)
at View.render (/opt/run/snapshot/package/node_modules/express/lib/view.js:76:8)
at Function.app.render (/opt/run/snapshot/package/node_modules/express/lib/application.js:504:10)
at ServerResponse.res.render (/opt/run/snapshot/package/node_modules/express/lib/response.js:798:7)
at exports.m2 (/opt/run/snapshot/package/routes/login.js:27:9)
at callbacks (/opt/run/snapshot/package/node_modules/express/lib/router/index.js:164:37)
I have nt changed anything about the route. I added a new route to the sme routing file which works, but should not affect this route). I haven't changes the view template in any way, or done anything I can think of to change the encoding. Given that Nodejitsu just takes my package.json and installs all the same modules, everything about the app should be identical.
I'm not sure how to track down the issue since the Nodejitsu logs are identical to the output error and I can't reproduce locally. I tried commenting out the new route, and it had no effect.

Frisby.js: Expected valid JavaScript object to be given, got undefined

I'm having some trouble getting a new test off the ground with this Frisby.js API testing framework.
For context, I have written some other tests that don't require reading in reference files from disk, and have run some of the samples that came with Frisby, and they all run very quickly and accurately. Really liking the speed of execution so far. Seeing as how all of that comes back OK, I am fairly certain that my environment is sound.
Below is a simplified version of a problematic JavaScript file, that I am running through jasmine-node:
var frisby = require('frisby');
var fs = require('fs');
var path = require('path');
var URL = 'http://server/api/';
var getJSON = fs.readFileSync(path.resolve(__dirname, 'GET.json'), 'utf-8').replace(/^\uFEFF/, ''); // the .replace removes the BOM from the start of the file
console.log(getJSON); // this dumps the file contents to the screen, no problems here
frisby.create('GET from API')
.get(URL + 'Endpoint?Parameters=Values')
.expectStatus(200) // tests that HTTP Status code equals expected 200, still no worries
.expectJSON(getJSON) // this is where the 'undefined' error is thrown
.toss();
Running the test from the command line is very straight forward:
C:\Testing\Frisby> jasmine-node apitest.js
I believe I'm doing things in the right order, with the synchronous file read, and then the Frisby calls afterwards, but when executed it throws the following error:
Failures:
1) Frisby Test: GET from API
[ GET http://server/api/Endpoint?Parameters=Values ]
Message:
TypeError: Expected valid JavaScript object to be given, got undefined
Stacktrace:
TypeError: Expected valid JavaScript object to be given, got undefined
at _jsonContains (C:\Users\jlucktay\AppData\Roaming\npm\node_modules\frisby\lib\frisby.js:1182:11)
at jasmine.Matchers.toContainJson (C:\Users\jlucktay\AppData\Roaming\npm\node_modules\frisby\lib\frisby.js:1141:12)
at null.<anonymous> (C:\Users\jlucktay\AppData\Roaming\npm\node_modules\frisby\lib\frisby.js:686:24)
at null.<anonymous> (C:\Users\jlucktay\AppData\Roaming\npm\node_modules\frisby\lib\frisby.js:1043:43)
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
Finished in 0.292 seconds
1 test, 2 assertions, 1 failure, 0 skipped
I have the jasmine-node and frisby packages both installed globally via npm, and have used npm link frisby which created the appropriate junction from my testing directory over to %APPDATA%\npm.
I have also tried changing the code around, to use fs.readFile instead of fs.readFileSync with the frisby calls inside the callback, but still have the same problem.
As I said above, my other tests and the samples that came with Frisby run and come back without error. Specifically, the httpbin_binary_post_put_spec.js sample uses almost identical code to what I ended up writing myself, and that sample works just fine.
I have routed the HTTP requests through Fiddler and can see the request and response, and everything looks fine there. It gets a HTTP 200 and the response body has the expected JSON that I want to compare against the file contents.
Why am I getting this error about an undefined object?
Rubber duck problem solving seems to have rescued me from my own stupidity.
The string needed to be turned into a proper JSON object:
.expectJSON(getJSON) -> .expectJSON(JSON.parse(getJSON))

Categories