I'm from a PHP background but trying out a Node/Express site for the first time. I've successfully used express-generator to get the basic skeleton running on localhost. I've also installed highcharts via npm and followed the instructions given by highcharts to add it into my project via require(). I now have this in my index.js:
var express = require('express');
var Highcharts = require('highcharts');
var router = express.Router();
// Load module after Highcharts is loaded
require('highcharts/modules/exporting')(Highcharts);
console.log(Highcharts);
// Create the chart
Highcharts.chart('container', { /*Highcharts options*/ });
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
module.exports = router;
I have two questions:
1) console.log() is not outputting to terminal or browser console when running DEBUG=project:* npm start. Is it outputting to something else I haven't checked or do I need to do something more to see this?
2) require('highcharts/modules/exporting')(Highcharts); is throwing TypeError: Cannot read property 'document' of undefined
at /Applications/MAMP/htdocs//node_modules/highcharts/modules/exporting.js:9:115
Where did I mess up?
The reason that the Highcharts example you linked to is using require()
is that many people run their browser code through Browserify, in order to keep using their favorite Node idioms and maintain some level of isomorphic design.
Despite that, Highcharts is still meant for the browser and cannot be used in Node. Your Express app is supposed to deliver an HTML page which itself loads Highcharts by some means. You aren't restricted to using Browserify, but it is a popular choice for this use case.
Also, as for your uncertainty about where to look for logs: always the terminal. Your Node programs will never log to the browser console, unless you go out of your way to make them do so using fancy things like Node Inspector.