0

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()?

3
  • var wikiatlas = require('path/to/wikiatlas.js') Commented Feb 18, 2015 at 11:57
  • added var wikiatlas = require('../js/wikiatlas.js') , same error. Is there a specific syntax to call function within the library (such locationMap()) ? Commented Feb 18, 2015 at 13:50
  • adding var wikiatlas = require('../js/wikiatlas.js') , is not enough, you must also convert your script into nodejs module.export format if you take this road. Also, it take aout the advantage of working with single code for client & server sides. Commented Feb 22, 2015 at 1:41

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.