javascript - Node.js/Express 不输出 console.log,highcharts.js 给出未定义的错误

标签 javascript node.js express highcharts express-generator

我有 PHP 背景,但第一次尝试 Node/Express 站点。我已成功使用express-generator 在本地主机上运行基本框架。我还通过 npm 安装了 highcharts 并遵循 instructions given by highcharts通过 require() 将其添加到我的项目中。我现在在我的 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;

我有两个问题:

1) 运行 DEBUG=project:* npm start 时,console.log() 不会输出到终端或浏览器控制台。它是否输出到其他我没有检查过的东西,或者我需要做更多的事情才能看到这个?

2) require('highcharts/modules/exporting')(Highcharts);抛出类型错误:无法读取未定义的属性“文档” 在/Applications/MAMP/htdocs//node_modules/highcharts/modules/exporting.js:9:115
我哪里搞砸了?

最佳答案

您链接到的 Highcharts 示例使用 require() 的原因是许多人通过 Browserify 运行浏览器代码。 ,以便继续使用他们最喜欢的 Node 习惯用法并保持一定程度的 isomorphic design .

尽管如此,Highcharts 仍然适用于浏览器,不能在 Node.js 中使用。您的 Express 应用程序应该提供一个 HTML 页面,该页面本身会通过某种方式加载 Highcharts。您不限于使用 Browserify,但它是此用例的热门选择。

此外,至于您不确定在哪里查找日志:始终是终端。你的 Node 程序永远不会登录到浏览器控制台,除非你不遗余力地使用像 Node Inspector 这样的奇特东西让它们这样做。 .

关于javascript - Node.js/Express 不输出 console.log,highcharts.js 给出未定义的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37083883/

相关文章:

javascript - 参数编码(引号和空格字符)

javascript - 从嵌套字典创建数组

node.js - npm Express install 正在添加额外的依赖项

node.js - 如何构建一个使用 React 构建的独立桌面应用程序,使用 Node 后端,并将前端和后端都 package 在 Electron 中作为桌面应用程序?

node.js - Nodejs Api 参数传递和控制台记录参数

javascript - 数字后加空格

javascript - 多个 Google map 无法与 Bootstrap 3 选项卡配合使用

javascript - Node.js:由于 URL 编码的 GET 参数而导致外部资源的路径困惑?

javascript - JQuery/JQuery UI 水平分隔线

node.js - Express.js 虚拟主机子域设置