【问题标题】:Node.js 5.5.0 console.log won't logNode.js 5.5.0 console.log 不会记录
【发布时间】:2016-05-11 12:22:36
【问题描述】:

当我在应用程序的任何位置调用 console.log("anything") 时,我无法获得任何终端输出。当我将节点更新到 5.5.0 时,问题似乎已经开始,在此之前我没有任何问题。这是怎么回事?我不确定还有哪些其他信息是相关的。我正在使用 express 运行标准 Node.js http 服务器。

这是我的 server.js 文件。现在有点丑……但无论如何:

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var passport = require('passport');
var flash = require('connect-flash');
var cors = require('cors');
var mongoose = require('mongoose');
var path = require('path');
var keys = require('./config/keys/apiKeys');

var morgan = require('morgan');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var validator = require('validator');
var fs = require('fs');
var ParseCloud = require('parse-cloud-express');
var Parse = ParseCloud.Parse;

require('console-stamp')(console, [])

var http = require('http').Server(app);
var https = require('https');

// config =========================================================================================

/*SSL */
var hskey = fs.readFileSync('./config/keys/theExperiment-key.pem');
var hscert = fs.readFileSync('./config/keys/theExperiment-cert.pem')

var options = {
    key: hskey,
    cert: hscert
};

var io = require('socket.io')(http);

var port = process.env.PORT || 8080;

app.use('/webhooks', ParseCloud.app);

require('./config/passport')(passport); //pass passport for configuration

app.use(bodyParser.json()); //get all data/stuff of the body (POST) parameters, parse application/json
app.use(bodyParser.json({type: 'application/vnd.api+json'})); //parse application/vnd.api+json as json
app.use(bodyParser.urlencoded({ extended: true })); //parse application/x-www-form-urlencoded
app.use(methodOverride("X-HTTP-Method-Override")); //override with the x-http-method-override header in the request. simulate delete/put
app.use(express.static(__dirname + '/public')); //set the static files location /public/img will be /img for users

//use template engine
app.set('views', __dirname + '/public/views/');
app.set('view engine', 'ejs');

//app.use(morgan('dev')); //log every request to the console
app.use(cookieParser()); //read cookies

//required for passport
app.use(session({
    secret: "secret",
    resave: true,
    saveUninitialized: true
})); //session secret and defaults
app.use(passport.initialize());
app.use(passport.session()); //persistent login sessions
app.use(flash()); //use connect-flash for flash messages stored in session
app.use(cors()); //allow cross origin resource sharing

Parse.initialize(keys.parse.applicationId, keys.parse.javascriptKey, keys.parse.masterKey); //Init Parse


// routes ===========================================================================================

require('./app/routes')(app, passport, io); //configure our routes, pass in app and passport

// socketIO =========================================================================================

require('./app/sockets')(io,passport); //require socketio control


// start app ========================================================================================

http.listen(port);
console.log('Hello ' + port);

//expose app
exports = module.exports = app;

这是我的 gruntfile.js(如 cmets 中所述):

// Gruntfile.js
module.exports = function(grunt) {

  grunt.initConfig({

    // JS TASKS ================================================================
    // check all js files for errors
    jshint: {
      all: ['public/src/**/*.js'] 
    },

    // take all the js files and minify them into app.min.js
    uglify: {
      build: {
        files: {
          'public/dist/js/app.min.js': ['public/src/**/*.js', 'public/src/*.js']
        }
      }
    },

    // CSS TASKS ===============================================================
    // process the less file to style.css
    less: {
      build: {
        files: {
          'public/dist/css/style.css': 'public/src/css/style.less'
        }
      }
    },

    //configure autoprefixing for compiled output css
    autoprefixer: {
      build: {
        files: {
          'public/dist/css/style.css': 'public/dist/css/style.css'
        }
      }
    },

    // take the processed style.css file and minify
    cssmin: {
      build: {
        files: {
          'public/dist/css/style.min.css': 'public/dist/css/style.css'
        }
      }
    },

    // COOL TASKS ==============================================================
    // watch css and js files and process the above tasks
    watch: {
      css: {
        files: ['public/src/css/**/*.less'],
        tasks: ['less', 'autoprefixer', 'cssmin']
      },
      js: {
        files: ['public/src/**/*.js'],
        tasks: ['jshint', 'uglify']
      }
    },

    // watch our node server for changes
    nodemon: {
      dev: {
        script: 'server.js',
        options: {
          max_old_space_size: "2048"
        }
      }
    },

    // run watch and nodemon at the same time
    concurrent: {
      options: {
        logConcurrentOutput: true
      },
      tasks: ['nodemon', 'watch']
    },

  });

  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-less');
  grunt.loadNpmTasks('grunt-autoprefixer');
  grunt.loadNpmTasks('grunt-contrib-cssmin');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-nodemon');
  grunt.loadNpmTasks('grunt-concurrent');

  grunt.registerTask('default', ['less', 'autoprefixer', 'cssmin', 'jshint', 'uglify', 'concurrent']);

};

【问题讨论】:

  • 这不太可能是 Node.js 5.5.0 中的错误。您的应用是否正常运行?
  • 它启动和运行没有问题吗?
  • 是的,似乎没有其他问题。我注意到的另一件事是,当使用 nodemon 运行时,输出曾经是“starting 'node server.js'”\n“server running”(或类似的东西)。现在“服务器正在运行”消息不再显示,尽管服务器确实启动并正常运行。
  • 摆脱一切,只需要一个 server.js 文件,其中包含 console.log ..
  • console-stamp 模块是什么?它对console 做了什么?

标签: javascript node.js terminal console.log


【解决方案1】:

隔离您的问题。 94% 的情况下,您没有看到控制台日志打印到终端,这是由于不同进程的标准输入和标准输出。

  1. 运行node server.js。 如果可行:
  2. 运行nodemon server.js。 如果可行:
  3. 完全删除console-stamp。 如果这不能解决问题:
  4. 从 grunt 文件默认值中删除 concurrent 并直接在链中运行 nodemon 任务。 如果这不能解决问题:
  5. 确保您在 package.json 中运行所有已安装依赖项的最新版本,并且它们都已更新并与您升级到的节点版本兼容。

【讨论】:

  • 谢谢,它最终是一个过时的nodemon版本,与节点5.5.0不兼容
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-05
  • 1970-01-01
  • 1970-01-01
  • 2014-10-21
  • 1970-01-01
  • 1970-01-01
  • 2020-10-21
相关资源
最近更新 更多