nodeJS phantom ReferenceError: Promise is not defined - javascript

Problem
I installed phantom and using node v0.10.26
I am getting a Promise error within phantom. I did not see any dependencies that needed to be installed.
Below is the sample code from phantom that I am using
/myProject/node_modules/phantom/lib/index.js:15
return new Promise(function (resolve) {
^
ReferenceError: Promise is not defined
at Object.module.exports.create (/Users/jbyrne/git/tin-validator/node_modules/phantom/lib/index.js:15:14)
at Object. (/Users/jbyrne/git/tin-validator/phantom.js:6:9)
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:902:3
I upgraded node to v0.11.13 and now get
$ node phantom.js
Exception: ReferenceError: Map is not defined
I installed phantom and using node v0.10.26
I am getting a Promise error within phantom. I did not see any dependencies that needed to be installed.
Below is the sample code from phantom that I am using
/myProject/node_modules/phantom/lib/index.js:15
return new Promise(function (resolve) {
^
ReferenceError: Promise is not defined
at Object.module.exports.create (/Users/jbyrne/git/tin-validator/node_modules/phantom/lib/index.js:15:14)
at Object. (/Users/jbyrne/git/tin-validator/phantom.js:6:9)
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:902:3
I upgraded node to v0.11.13 and now get
$ node phantom.js
Exception: ReferenceError: Map is not defined
Code
var phantom = require('phantom');
var sitepage = null;
var phInstance = null;
phantom.create()
.then(function(instance) {
phInstance = instance;
return instance.createPage();
})
.then(function(page) {
sitepage = page;
return page.open('https://stackoverflow.com/');
})
.then(function(status) {
console.log(status);
return sitepage.property('content');
})
.then(function(content) {
console.log(content);
sitepage.close();
phInstance.exit();
})["catch"](function(e) {
console.error('Exception: %s', e);
});

Your version of PhantomJS depends on some global objects that don't exist in Node 0.10. You can either upgrade Node to the latest stable version, or downgrade your PhantomJS to 1.x.
If you're switching between Node versions, you can do so easily with nvm.

upgrade node to 0.12
the error mentioned is due to the line new Map() in phantom.
the Map constructor was added in node 0.12

Related

Error: Cannot find module '#rails/webpacker' rails 6

When I run my Rails 6.0 application on development environment I get the next trace on the development.log
[Webpacker] Compiling…
[Webpacker] Compilation failed:
/home/alejo/rails_project/node_modules/webpack-cli/bin/cli.js:93
throw err;
^
Error: Cannot find module '#rails/webpacker'
Require stack:
- /mnt/config/webpack/environment.js
- /mnt/config/webpack/development.js
- /home/alejo/rails_project/node_modules/webpack-cli/bin/utils/convert-argv.js
- /home/alejo/rails_project/node_modules/webpack-cli/bin/cli.js
- /home/alejo/rails_project/node_modules/webpack/bin/webpack.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:954:17)
at Function.Module._load (internal/modules/cjs/loader.js:847:27)
at Module.require (internal/modules/cjs/loader.js:1016:19)
at require (/home/alejo/rails_project/node_modules/webpack-cli/node_modules/v8-compile-cache/v8-compile-cache.js:161:20)
at Object.<anonymous> (/mnt/config/webpack/environment.js:1:87)
at Module._compile (/home/alejo/rails_project/node_modules/webpack-cli/node_modules/v8-compile-cache/v8-compile-cache.js:192:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1160:10)
at Module.load (internal/modules/cjs/loader.js:976:32)
at Function.Module._load (internal/modules/cjs/loader.js:884:14)
at Module.require (internal/modules/cjs/loader.js:1016:19)
at require (/home/alejo/rails_project/node_modules/webpack-cli/node_modules/v8-compile-cache/v8-compile-cache.js:161:20)
at Object.<anonymous> (/mnt/config/webpack/development.js:3:21)
at Module._compile (/home/alejo/rails_project/node_modules/webpack-cli/node_modules/v8-compile-cache/v8-compile-cache.js:192:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1160:10)
at Module.load (internal/modules/cjs/loader.js:976:32)
at Function.Module._load (internal/modules/cjs/loader.js:884:14)
at Module.require (internal/modules/cjs/loader.js:1016:19)
at require (/home/alejo/rails_project/node_modules/webpack-cli/node_modules/v8-compile-cache/v8-compile-cache.js:161:20)
at WEBPACK_OPTIONS (/home/alejo/rails_project/node_modules/webpack-cli/bin/utils/convert-argv.js:114:13)
at requireConfig (/home/alejo/rails_project/node_modules/webpack-cli/bin/utils/convert-argv.js:116:6)
at /home/alejo/rails_project/node_modules/webpack-cli/bin/utils/convert-argv.js:123:17
at Array.forEach (<anonymous>)
at module.exports (/home/alejo/rails_project/node_modules/webpack-cli/bin/utils/convert-argv.js:121:15)
at /home/alejo/rails_project/node_modules/webpack-cli/bin/cli.js:71:45
at Object.parse (/home/alejo/rails_project/node_modules/webpack-cli/node_modules/yargs/yargs.js:567:18)
at /home/alejo/rails_project/node_modules/webpack-cli/bin/cli.js:49:8
at Object.<anonymous> (/home/alejo/rails_project/node_modules/webpack-cli/bin/cli.js:366:3)
at Module._compile (internal/modules/cjs/loader.js:1121:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1160:10)
at Module.load (internal/modules/cjs/loader.js:976:32) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/mnt/config/webpack/environment.js',
'/mnt/config/webpack/development.js',
'/home/alejo/rails_project/node_modules/webpack-cli/bin/utils/convert-argv.js',
'/home/alejo/rails_project/node_modules/webpack-cli/bin/cli.js',
'/home/alejo/rails_project/node_modules/webpack/bin/webpack.js'
]
}
I am using webpacker 4.0.7, rails 6.0.0 and ruby 2.6.5
I want to know why am I getting this error and how do I solve it.
Thank you
I ran into this issue as well. For me, the issue was the node version (13.6.0). I figured this out by running yarn add #rails/webpacker, which returned the error Expected version "^6 || ^7 || ^8 || ^9 || ^10 || ^11 || ^12 || >=13.7". Got "13.6.0"
To fix it:
Update node to an appropriate version (see error above)
Delete node_modules directory
Update yarn files with yarn install --check-files
Install webpacker with rails webpacker:install
Confirm it worked by bin/webpack-dev-server and getting no errors

Can't use a class defined in an npm package

I am trying to use a typescript-parser:
https://www.npmjs.com/package/typescript-parser
but I'm having difficulties with using the package properly.
I installed the package successfully using npm install; when I run npm list --depth=0 it shows up in the list.
Eventually, I tried running the following line of code that is suggested in the link attached above (using node command):
const parser = new TypescriptParser();
but the problem is that I get the following error:
const parser = new TypescriptParser();
^
ReferenceError: TypescriptParser is not defined
at Object.<anonymous> (C:\Users\...\parser_example.js:3:16)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
Alternatively, I tried running this:
const TypescriptParser = require('typescript-parser');
const parser = new TypescriptParser();
yet I still get a (different) error:
const parser = new TypescriptParser();
^
TypeError: TypescriptParser is not a constructor
at Object.<anonymous> (C:\Users\...\parser_example.js:3:16)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
It seems as if although the package was installed, it doesn't recognize the class TypescriptParser.
Any help would be much appreciated!
Try importing using:
import { TypescriptParser } from 'typescript-parser';
I tried the code below and it works for me :
index.js file
const parser = require('typescript-parser');
const parser = new TypescriptParser();
I run it with node index.js
This code works for me:
index.js file
const tp = require('typescript-parser');
const parser = new tp.TypescriptParser();

Init is a not a function

I have one application which are used node js. In which I have used selenium webdriverIO for testing purpose but when I write a script using selenium webdriverIO it and using INIT function it will generate error "TypeError:INIT is not a function". Please give solution for this code
TypeError: webdriverio.remote(...).init is not a function
at Object. (E:\WebdriverIOAutomation\test\specs\WithoutWdioClient.js:11:6)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
var webdriverio = require('webdriverio');
var options = {
desiredCapabilities: {
browserName: 'chrome'
}
};
webdriverio
.remote(options)
.init()
.url('http://www.google.com')
.title(function(err, res) {
console.log('Title was: ' + res.value);
})
.end();
Expected Result: Get Title of Google
Actual Result: Generate Error of INIT Function

How to run jQuery in node.js [duplicate]

This question already has answers here:
Can I use jQuery with Node.js?
(21 answers)
Closed 4 years ago.
I have tried npm install jQuery --save and in my node file var $ = require('jquery'); but, then when I run my node file with
$.getJSON('https://en.wikipedia.org/wiki/Washington,_D.C.', function(data) {
//data is the JSON string
});
I get the error
TypeError: $.getJSON is not a function
at Object.<anonymous> (C:\Users\Karim\node 2\tweetPic.js:16:3)
at Module._compile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Function.Module.runMain (module.js:684:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
I have also tried importing jquery using
require("jsdom").env("", function(err, window) {
if (err) {
console.error(err);
return;
}
var $ = require("jquery")(window);
});
which just returns
TypeError: require(...).env is not a function
at Object.<anonymous> (C:\Users\Karim\node 2\tweetPic.js:3:18)
at Module._compile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Function.Module.runMain (module.js:684:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
I have installed the jsdom package in a similar fashion. Is there something wrong with my jquery code itself? How can I fix this?
Edit: It seems jQuery isn't really what I need here. I'm just going to look into a different way of retrieving json data.
Sorry but i don't know how to install jquery. But you apparently need it to request a website and fetch json. You could use request for that. Hope i helped you.
And you could do like :
request('https://en.wikipedia.org/wiki/Washington,_D.C.', function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
});

node.js cannot find 'neo4j' module (Thingdom)

After many tries, I am unnable to connect node.js to Neo4j installed in my computer. I am able to access both separately, and both work fine. I have install in my Node.js directory the Thingdom ('neo4j') module in the directory, but when require('neo4j') prints an error.
Image of my Node.js folder with Neo4j installed in modules
var neo4j = require("neo4j");
var db = new neo4j.GraphDatabase("http://localhost:7474");
var node = db.createNode({hello: 'world'}); // instantaneous, but...
node.save(function (err, node) { // ...this is what actually persists.
if (err) {
console.error('Error saving new node to database:', err);
} else {
console.log('Node saved to database with id:', node.id);
}
});
And when using in the cmd: "node index.js" it throws me this error:
C:\Users\RRamos\Documents\Projects\test-neo4j>node index.js
module.js:341
throw err;
^
Error: Cannot find module 'neo4j'
at Function.Module._resolveFilename (module.js:339:15)
at Function.Module._load (module.js:290:25)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at Object.<anonymous> (C:\Users\RRamos\Documents\Projects\test-neo4j\index.js:1:75)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
I got the same problem. As there's no solution in this post, I just add mine.
After running $ npm init and $ npm install --save neo4j-driver, I copy and paste the neo4j example code in index.js:
var neo4j = require("neo4j");
var db = new neo4j.GraphDatabase('http://neo4j:<password>#localhost:7474');
And then I got the same error when running $ node index.js.
In my package.json, I found:
"dependencies": {
"neo4j-driver": "^1.1.0-M02"
}
It's neo4j-driver not neo4j. So replace it in index.js:
var neo4j = require("neo4j-driver");
var db = new neo4j.GraphDatabase('http://neo4j:<password>#localhost:7474');
Now you will get rid of the Cannot find module 'neo4j' error!
In addition, if you use the 1.1.0 version of neo4j-driver(for Neo4j 3.0.0+), you may get this error:
var db = new neo4j.GraphDatabase('http://neo4j:<password>#localhost:7474');
^
TypeError: neo4j.GraphDatabase is not a constructor
at Object.<anonymous> (D:\Codes\neo4j_test\server.js:2:10)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.runMain (module.js:575:10)
at run (bootstrap_node.js:352:7)
at startup (bootstrap_node.js:144:9)
at bootstrap_node.js:467:3
It seems neo4j.GraphDatabase is only available in older version of neo4j-driver.
Here's the up-to-date tutorial of neo4j-driver.
Use the following code instead:
var neo4j = require('neo4j-driver').v1;
var driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "<password>"));
// Create a session to run Cypher statements in.
// Note: Always make sure to close sessions when you are done using them!
var session = driver.session();
// Run a Cypher statement, reading the result in a streaming manner as records arrive:
session
.run("MERGE (alice:Person {name : {nameParam} }) RETURN alice.name", { nameParam:'Alice' })
.subscribe({
onNext: function(record) {
console.log(record._fields);
},
onCompleted: function() {
// Completed!
session.close();
},
onError: function(error) {
console.log(error);
}
});

Categories

Resources