node.js - Issue with npm require -


i'm running nightmare.js script, , within i'm trying require lodash (installed locally npm, in node_modules, registered in package.json).

index.js :

var nightmare = require('nightmare'); var _ = require('lodash');  function getrandomint(min, max) {   min = math.ceil(min);   max = math.floor(max);   return math.floor(math.random() * (max - min)) + min; }  function getelms(elm) {     var elsm =  document.queryselectorall(elm);     return _.map(elsm, function(elem){ return elem.innerhtml; }); }   var somewebsite = new nightmare({ show: false })   .goto( ***some url*** )   .wait(getrandomint(2500, 5000))   .evaluate(function () {        var elsm =  document.queryselectorall(***some selector***);       return _.map(elsm, function(elem){ return elem.innerhtml; });        // return getelms(***some selector***);     }).then(function(linkstexts) {     console.log(linkstexts)   }); 
  • but i'm getting this:

    unhandledpromiserejectionwarning: unhandled promise rejection (rejection id: 1): _ not defined

    • similar issue if uncomment , use return getelms(***some selector***);

this package.json

{   "name": "nightxp",   "version": "1.0.0",   "description": "",   "main": "index.js",   "dependencies": {     "lodash": "^4.17.4",     "nightmare": "^2.10.0"   },   "devdependencies": {},   "scripts": {     "test": "echo \"error: no test specified\" && exit 1"   },   "author": "",   "license": "isc" } 

enter image description here

what missing?

update

i tried passing lodash browser scope, , completing catch(), somehow lodash variable still isnt being passed scope (example hackernews):

var nightmare = require('nightmare'); var lodash = require('lodash');  function getrandomint(min, max) {   min = math.ceil(min);   max = math.floor(max);   return math.floor(math.random() * (max - min)) + min;  }  var facebook = new nightmare({ show: false })   .goto('https://news.ycombinator.com')   .wait(getrandomint(5500, 6000))   .evaluate(  (lodash) => {       var elsm =  document.queryselectorall('tr td.title a.storylink');       return lodash.map(elsm, function(elem){ return elem.innerhtml; });    }, lodash).then(function(titles) {     console.log(titles)   }).catch(function (error) {       console.error('error:', error);   }); 

and get:

c:\projects\nightmare>set debug=nightmare & node index.js   nightmare queuing process start +0ms   nightmare queueing action "goto" https://news.ycombinator.com +4ms   nightmare queueing action "wait" +2ms   nightmare queueing action "evaluate" +0ms   nightmare running +1ms error: cannot read property 'map' of undefined 

when run

var somewebsite = new nightmare({ show: false }) .evaluate(function () { /* ... */ } 

you in browser scope — can think of nightmare.evaluate running contained code in context of independent browser, means global scope, including _ not available.

see here bit more info , tips on passing data scope:

https://github.com/segmentio/nightmare/issues/89

https://github.com/segmentio/nightmare/issues/333

edit:

while can pass simple values evaluate node context, can't pass complex values functions , methods. because arguments serialize in transit , difficult functions. here's issue explains bit:

https://github.com/segmentio/nightmare/issues/349

in particular case seems easiest thing work without lodash, these days isn't hard. example:

var facebook = new nightmare({ show: false }) .goto('https://news.ycombinator.com') .wait(getrandomint(5500, 6000)) .evaluate(  () => {     var elsm =  document.queryselectorall('tr td.title a.storylink');     return array.prototype.map.call(elsm, elem => elem.innerhtml) }) .then(function(titles) {     console.log("title", titles) }) .catch(function (error) {     console.error('error:', error); }); 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -