Hugolpz
Hugolpz

Reputation: 18248

"ReferenceError: <functionName> is not defined" : libraries issue?

I'am very, very new to node.js and I'am likely to ask a quite easy questions.

Client side: I've been able to make good maps on client side via d3js, based on a custom made ./js/wikiatlas.js (here).

Server side: i now want to move this map generation server side. I was successful outputing a basic square.svg using node, jsdom, and d3js. I now want to use more complex functions from ./js/wikiatlas.js. So I naively added the ./js/wikiatlas.js dependency and the js function call locationMap("body",800, target, title, WEST, NORTH, EAST, SOUTH); expecting it to work (the other things works for sure). My full code:

var jsdom = require('jsdom');
var fs    = require('fs');
jsdom.env(
  "<html><body></body></html>",        // CREATE DOM HOOK:
  [ '../js/jquery-2.1.3.min.js', 
  '../js/d3.v3.min.js', 
  '../js/topojson.v1.min.js', 
  '../js/wikiatlas.js',             // <<======== my code (IMPORTANT !!)
  '../js/b64.js'],                  // ... & offline
//'http://rugger-demast.codio.io/js/wikiatlas.js',
  function (err, window) {
/* COLLECT ENV.VARIABLES ******************************************* */
    var WEST  = process.env.WEST,     
        NORTH = process.env.NORTH,
        EAST  = process.env.EAST,
        SOUTH = process.env.SOUTH,
        target= process.env.ITEM,
        title = process.env.ITEM,
        WIDTH = process.env.WIDTH;

/* D3js FUNCTION *************************************************** */
locationMap("body",800, target, title, WEST, NORTH, EAST, SOUTH); // <<======== defined in wikiatlas.js!

/* SVG PRINT ******************************************************* */
    var svgheader = '<?xml version="1.0" encoding="utf-8"?>'
    +'<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">';
    fs.writeFileSync('administrative_map_(wikiatlas_2014).svg', svgheader + window.d3.select("body").html());
    // console.log(window.d3.select("body").html());
 }
// END (D3JS) * * * * * * * * * * * * * * * * * * * * * * * *
);

But I then get that ReferenceError: locationMap is not defined error:

/data/yug/projects_active/make-modules/09_d3/location.node.js:30
locationMap("body",800, target, title, WEST, NORTH, EAST, SOUTH);
^
ReferenceError: locationMap is not defined
    at Object.done (/data/yug/projects_active/make-modules/09_d3/location.node.js:30:1)
    at /data/yug/projects_active/make-modules/node_modules/jsdom/lib/jsdom.js:270:18
    at process._tickCallback (node.js:419:13)
make: *** [output] Error 8

How should I load or frame ./js/wikiatlas.js so I could use functions inside of it on my node.js / jsdom server script ?


Seems related to : In Node.js, how do I "include" functions from my other files? , Nodejs script fails to print after d3.json()?

Upvotes: 0

Views: 1302

Answers (1)

Sebmaster
Sebmaster

Reputation: 569

Since you load your script into the window's context you also have to access it with the window, i.e. window.locationMap.

jsdom.env(
  "<html><body></body></html>",        // CREATE DOM HOOK:
  [ '../js/jquery-2.1.3.min.js', 
  '../js/d3.v3.min.js', 
  '../js/topojson.v1.min.js', 
  '../js/wikiatlas.js',             // <<======== my code (IMPORTANT !!)
  '../js/b64.js'],

then

window.locationMap("body",800, target, title, WEST, NORTH, EAST, SOUTH);

that's it.

Upvotes: 1

Related Questions