NodeJS - To perform request, define both url and callback - node.js

So I have been starting to get Nodejs to work and it seems like it is not going my way. I have been trying to learn using cloudscraper This code to be exact and whenever I run it with command node test.js it always return a error of
#!/usr/bin/env node
/* eslint-disable promise/always-return */
var cloudscraper = require('cloudscraper');
var fs = require('fs');
cloudscraper.get({ uri: 'https://subscene.com/content/images/logo.gif', encoding: null })
.then(function (bufferAsBody) {
fs.writeFileSync('./test.gif', bufferAsBody);
})
.catch(console.error);
Error: To perform request, define both url and callback
at performRequest (C:\Users\node_modules\cloudscraper\index.js:84:11)
at Object.cloudscraper.get (C:\Users\node_modules\cloudscraper\index.js:17:3)
at Object.<anonymous> (C:\Users\test.js:7:14)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
At this point I dont know what I am doing wrong and here I am asking what I am doing wrong?
EDIT:
Im leaving this thread but I fixed it by removing the node_modules and reinstalled from the beginning!

Did you try a minimalist version?
var cloudscraper = require('cloudscraper');
cloudscraper.get('https://subscene.com/content/images/logo.gif')
.then(function (bufferAsBody) {
console.log('success');
})
.catch(console.error);

Related

How to fix SyntaxError: Unexpected token ... while using socket.io in my local machine

I am trying to use socket.io on one of a simple project but when I run the same on my local machine I am getting this bug. I am not able to figure out what is going wrong my index.js is as follows
const app = require("express")();
const http = require("http").Server(app);
const io = require("socket.io")(http);
const port = process.env.PORT || 8080;
app.get("/", function(req, res) {
res.render("index.ejs");
});
http.listen(port, function() {
console.log("Listening on *:" + port);
});
The full log is here
Desktop/chat/node_modules/ws/lib/websocket.js:347
...options
^^^
SyntaxError: Unexpected token ...
at createScript (vm.js:74:10)
at Object.runInThisContext (vm.js:116:10)
at Module._compile (module.js:533:28)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Module.require (module.js:513:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (Desktop/chat/node_modules/ws/index.js:3:19)
My node version is 8.0
This looks like it's probably the object spread operator. If you showed the actual line of code that causes the error, we could see for sure.
You will need at least node v8.6 to get support for the object spread operator by default. Starting with node v8.21, it could be enabled via command line flags.

Node test is not running

I am trying to run following code===>
var webdriverio = require('webdriverio');
var options = { desiredCapabilities: { browserName: 'chrome' } };
var client = webdriverio.remote(options);
client
.init()
.url('http://www.webdriveruniversity.com/')
.click('#login-portal')
.getTitle().then(function(title) {
console.log('Title is: ' + title);
})
.end();
The output I am getting as follows. Not sure how to solve it.
const remote = async function (params = {}, remoteModifier) {
^^^^^^^^
SyntaxError: Unexpected token function
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (C:\Users\user\Desktop\webdriverFramework\loginPortal
Test.js:1:81)
It seems you are trying to use a library that uses async functions with an old Node.js version that does not support them. Please, run node -v and compare the Node.js version with the async functions support table.

Node mocha test case is not running

I am trying to run the following code with node.
var webdriverio = require('webdriverio');
var options = { desiredCapabilities: { browserName: 'chrome' } };
var client = webdriverio.remote(options);
client
.init()
.url('http://www.webdriveruniversity.com/')
.click('#login-portal')
.getTitle().then(function(title) {
console.log('Title is: ' + title);
})
.end();
I am getting this error
DEBUG wdio-config: #wdio/sync not found, runn
ing tests asynchronous
C:\Users\Adnan\Desktop\webdriverFramework\loginPortalTest.js:6
.init()
^
TypeError: client.init is not a function
at Object.<anonymous> (C:\Users\Adnan\Desktop\webdriverFramework\l
oginPortalTest.js:6:6)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:7
00:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
How can I resolve this? Previously I had the issue with version. But I upgraded to new version.
If I understand correctly, you lack #wdio/sync and your runner toggles into asynchronous mode. So webdriverio.remote(options) returns promise that has not .init() method.

Error in async, in Koa?

I want to implement an API with POST method, But when i am starting my server then Error occured:
3450:~/Desktop/koa/ctx$ node koa2.js
/home/358/Desktop/koa/ctx/koa2.js:9
router.post('/locations', async (ctx, next) =>{
^
SyntaxError: Unexpected token (
at Object.exports.runInThisContext (vm.js:76:16)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
358#daffolap358-Latitude-3450:~/Desktop/koa/ctx$
Can anyone tell me where I am doing wrong ?
My code is :
server.js:
var Koa =require('koa');
var middleware =require('koa-router');
var logger =require('koa-logger');
var parser =require('koa-bodyparser');
const router = middleware();
const app = new Koa();
router.post('/locations', async (ctx, next) =>{
console.log("ctx");
});
app
.use(logger()) // Logs information.
.use(parser()) // Parses json body requests.
.use(router.routes()) // Assigns routes.
.use(router.allowedMethods())
app.listen(5050, () => console.log('Listening on port 5050.'));
export default app;
You are getting this error because your node version is less than 7.6.0.
It should be greater than 7.6 also Async/await support in Node 7.6 comes from updating V8, Chromium’s JavaScript engine, to version 5.5.
Check your Node version. It should be >= 7.6.0. Otherwise koa-2 and async-await pattern will not work.

Lab returns error no method 'experiment'

I am writing the test code for a function that will check for the existence of a username in twitter. I am using the Hapi framework and Lab for testing.
When I run npm test I get the following error:
> NameGen#0.0.0 test /Users/mario/projects/NameGen
> ./node_modules/lab/bin/lab -c
/Users/mario/projects/NameGen/test/test.js:5
Lab.experiment( "Test Username Existence", function() {
^
TypeError: Object #<Object> has no method 'experiment'
at Object.<anonymous> (/Users/mario/projects/NameGen/test/test.js:5:5)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js [as origLoader] (module.js:474:10)
at Object.require.extensions..js (/Users/mario/projects/NameGen/node_modules/lab/lib/coverage.js:32:26)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at /Users/mario/projects/NameGen/node_modules/lab/lib/cli.js:85:23
at Array.forEach (native)
at Object.internals.traverse (/Users/mario/projects/NameGen/node_modules/lab/lib/cli.js:81:19)
at Object.exports.run (/Users/mario/projects/NameGen/node_modules/lab/lib/cli.js:30:29)
at Object.<anonymous> (/Users/mario/projects/NameGen/node_modules/lab/bin/lab:3:23)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
npm ERR! Test failed. See above for more details.
npm ERR! not ok code 0
My test code is:
var Lab = require("lab");
var server = require('../'); // require index.js
var request = require('request');
Lab.experiment( "Test Username Existence", function() {
// tests
Lab.test( "Test username exists", function(done){
var options = {
url: 'http://twitter.com/BarackObama'
};
request(options, function(error, response, body){
Lab.expect(response.statusCode).to.equal(200);
});
done();
});
Lab.test("Test username does not exists", function(done){
var options = {
url: 'http://twitter.com/jhkhksdhkjahsdfkjhasdf'
};
request(options, function(error, response, body){
Lab.expect(response.statusCode).to.equal(404);
});
done();
});
});
I have the latest version of node installed and all the necessary dependencies in my package.json file. What could be causing this error?
As #Jordonias had stated. However, when I used his example, it still did not work.
I had to change the 2nd line to:
lab = exports.lab = Lab.script()
Per the Lab docs: Test files must require the lab module, and export a test script
test.js
var Lab = require('lab');
var lab = module.exports = Lab.script();
lab.experiment( "Test Username Existence", function() {
lab.test( "Test username exists", function(done){
});
});

Resources