【问题标题】:nodemon app crashed - waiting for file changes before startingnodemon 应用程序崩溃 - 在启动前等待文件更改
【发布时间】:2016-09-25 23:30:26
【问题描述】:

编辑 经过进一步测试,我发现在这个应用程序和默认安装的 mean.js 上 gulp 和 grunt 都会发生这种情况。我在 Mac 上本地运行它。当我使用“node server.js”运行任一应用程序时,它们都不会崩溃。

我正在使用带有 grunt-nodemon 的 MEAN 堆栈,并且在访问快速 URL 时节点崩溃。但它并不总是一致的。有时它可以工作,有时当 URL 被击中时节点崩溃,没有数据,而其他时候我得到响应并且节点在之后立即崩溃。

浏览器控制台响应:

http://localhost:8000/api/users net::ERR_CONNECTION_REFUSED

终端输出:

Mongoose: users.insert({ firstname: 'mike', lastname: 'jones', email:'mike@gmail.com', role: 'admin', password: 'mike', _id: ObjectId("57485c16fc11894b96c28057"), created: new Date("Fri, 27 May 2016 14:39:18 GMT"), __v: 0 })   
user.save success
node crash
[nodemon] app crashed - waiting for file changes before starting...

在这种情况下,POST 请求通过,用户被添加,然后节点崩溃,但有时它在成功 POST 之前崩溃。 Node 有时也会在 GET 请求上崩溃。

gruntfile.js:

module.exports = function(grunt) {
    // Load grunt tasks automatically
    require('load-grunt-tasks')(grunt);

    var pkg = grunt.file.readJSON('package.json');

    var options = {
        paths: {
            app: 'app',
            assets: 'app/assets',
            dist: 'app/dist',
            distAssets: 'app/dist/assets',
            html: 'app/html',
            htmlTmp: '.tmp/htmlsnapshot',
            htmlAssets: 'app/html/assets',
            index: 'app/dist/index.html',
            indexDev: 'app/index.html',
            indexTmp: '.tmp/html/index.html'
        },
        pkg: pkg,
        env: {
            test: {
                NODE_ENV: 'test'
            },
            dev: {
                NODE_ENV: 'development'
            },
            prod: {
                NODE_ENV: 'production'
            }
        }
    };

    // Load grunt configurations automatically
    var configs = require('load-grunt-configs')(grunt, options);

    // Define the configuration for all the tasks
    grunt.initConfig(configs);

    // Connect to the MongoDB instance and load the models
    grunt.task.registerTask('mongoose', 'Task that connects to the MongoDB instance and loads the application models.', function () {
        // Get the callback
        var done = this.async();

        // Use mongoose configuration
        var mongoose = require('./config/lib/mongoose.js');

        // Connect to database
        mongoose.connect(function (db) {
            done();
        });
    });

    grunt.registerTask('bumper', ['bump-only']);
    grunt.registerTask('css', ['sass']);
    grunt.registerTask('default', [
        'sass',
        'copy:dev',
        'nodemon',
        'concurrent:dev',
        'watch',
        'mongoose'
    ]);

    grunt.registerTask('shared', [
        'clean:demo',
        'copy:demo',
        'sass',
        'ngconstant',
        'useminPrepare',
        'concat:generated',
        'cssmin:generated',
        'uglify:generated',
        'filerev',
        'usemin',
        'imagemin',
        'usebanner'
    ]);

    grunt.registerTask('demo', [
        'shared',
        'copy:postusemin',
        'grep:demo'
    ]);

    grunt.registerTask('dist', [
        'shared',
        'copy:postusemin',
        'copy:dist',
        'grep:dist',
        'compress',
        'copy:postusemin',
        'grep:demo',
    ]);

    grunt.loadNpmTasks('grunt-forever');

};

default.js

module.exports.tasks = {
    // version update
    bump: {
        options: {
            files: ['package.json', 'bower.json'],
            pushTo: 'origin'
        }
    },

    // application constants
    ngconstant: {
        options: {
            dest: '<%= paths.assets %>/js/app.constants.js',
            name: 'app.constants',
        }
    },

    // remove all bs from css
    cssmin: {
        options: {
            keepSpecialComments: 0
        }
    },
    markdown: {
        all: {
            files: [
                {
                    src: 'README.md',
                    dest: '<%= paths.assets %>/tpl/documentation.html'
                }
            ],
            options: {
                template: '<%= paths.assets %>/tpl/_documentation_template.html',
            }
        }
    }
};

dev.js:

var _ = require('lodash'),
defaultAssets = require('./assets/default'),
testAssets = require('./assets/test'),
testConfig = require('./env/test'),
fs = require('fs'),
path = require('path');

module.exports.tasks = {
    // copy files to correct folders
    copy: {
        dev: {
            files: [
                { expand: true, src: '**', cwd: '<%= paths.app %>/bower_components/font-awesome/fonts',                    dest: '<%= paths.assets %>/fonts' },
                { expand: true, src: '**', cwd: '<%= paths.app %>/bower_components/material-design-iconic-font/fonts',     dest: '<%= paths.assets %>/fonts' },
                { expand: true, src: '**', cwd: '<%= paths.app %>/bower_components/roboto-fontface/fonts',                 dest: '<%= paths.assets %>/fonts' },
                { expand: true, src: '**', cwd: '<%= paths.app %>/bower_components/weather-icons/font',                    dest: '<%= paths.assets %>/fonts' },
                { expand: true, src: '**', cwd: '<%= paths.app %>/bower_components/bootstrap-sass/assets/fonts/bootstrap', dest: '<%= paths.assets %>/fonts' }
            ]
        }
    },

    // watch for changes during development
    watch: {
        js: {
            files: ['Gruntfile.js', '<%= paths.assets %>/js/**/*.js'],
            tasks: ['jshint'],
            options: {
                livereload: true
            }
        },
        css: {
            files: [
                '<%= paths.assets %>/css/**/*.scss'
            ],
            tasks: ['sass'],
            options: {
                livereload: true
            }
        },
        markdown: {
            files: [
                'README.md'
            ],
            tasks: ['markdown']
        },
        tasks:  [ 'express:dev' ],
    },

    // debug while developing
    jshint: {
        all: ['Gruntfile.js', '<%= paths.assets %>/js/**/*.js']
    },
    concurrent: {
        dev: {
            tasks: ['nodemon', 'node-inspector', 'watch'],
            options: {
                logConcurrentOutput: true
            }
        }
    },
    nodemon: {
        dev: {
            script: 'server.js',
            options: {
                nodeArgs: ['--debug'],
                ext: 'js,html',
                callback: function (nodemon) {

                    nodemon.on('crash', function (event) {
                        console.log(event);
                    });


                },
                watch: _.union(defaultAssets.server.gruntConfig, defaultAssets.server.views, defaultAssets.server.allJS, defaultAssets.server.config)
            }
        }
    },
    forever: {
        server1: {
            options: {
                index: 'server.js',
                //logDir: 'logs'
            }
        }
    }
};

角度控制器功能:

  $scope.addUser = function(){

      var user = {
          firstname: $scope.firstname,
          lastname: $scope.lastname,
          email: $scope.email,
          role: $scope.role.selected,
          password: $scope.password
      };

      $http.post('/api/userAdd', user ).then(function successCallback(response) {
          $location.path('/users');
      }, function errorCallback(response) {
          console.log('error addding user');
          console.log(response);
      });
  };

快车路线:

User = require('../models/user.js');

module.exports = function (app) {

    app.get('/api/users', function (req, res) {

        User.find({}, function (err, users) {
            if ( err ) {
                res.send({
                    message : 'error finding users',
                    success: false
                });
            } else {
                res.json(users);
            }
        });

    });

    app.get('/api/users', function (req, res) {
        User.find({fields: {}}, function (err, docs) {
            res.json(docs);
        });
    });

    app.post('/api/userAdd', function (req, res) {

        var user = new User(req.body);

        user.save( function( err, user ){

            if (err){
                console.log('user.save error');
                console.log(err);
                res.send({
                    success: false
                });
            } else {
                console.log('user.save success');
                res.send({
                    success: true
                });
            }
        });

    });

};

我还在使用 Chromes Advanced REST 扩展进行测试,使用此工具的任何请求节点都会立即崩溃。

我是 MEAN 的新手,所以我在这里遗漏了导致崩溃的东西吗?有任何想法吗?

【问题讨论】:

  • 尝试手动运行您的服务器,看看是否在崩溃之前打印出任何错误:node server.js
  • 手动运行似乎可以解决问题,谢谢。这是否意味着我对 Grunt 有一些问题或配置错误?
  • 并非如此。我假设通过独立运行它,您会看到 为什么 它会崩溃。它不应该以这种方式更好地工作;D
  • 添加了正在使用并导致错误的完整 gruntfile.js、default.js 和 dev.js。希望能有所帮助。
  • 现在使用node --debug server.js 进行测试我收到错误消息Segmentation fault: 11 并且节点崩溃。但是如果我不使用--debug 就不会崩溃,也不会出现错误消息

标签: angularjs node.js mean-stack grunt-nodemon


【解决方案1】:

我将节点从 v4.2.6 更新到 v4.4.5,解决了默认安装 mean.js 的问题。我会从那里建立起来。

【讨论】:

  • 我建议你切换到最后一个节点 LTS 版本:8.9.3。或者至少是最后一个 LTS 节点 4.x:4.8.7。使用 nvm (github.com/creationix/nvm) 轻松移动。
【解决方案2】:

我在开发 nexmo 短信应用时遇到了这个挑战。

要修复崩溃问题,请转至您的 app.js。如果你有这些代码行。它通常在第一行...

`import { Socket } from 'dgram';`

`const express = require('express');`
`const bodyParser = require('body-parser');`
`const ejs = require('ejs');`
`const Nexmo = require('nexmo');`
`const socketio = require('socket.io');`

只需删除/移除第一行代码 import { Socket } from 'dgram';

留下这些......

    `const express = require('express');`
    `const bodyParser = require('body-parser');`
    `const ejs = require('ejs');`
    `const Nexmo = require('nexmo');`
    `const socketio = require('socket.io');`

并使用“nodemon”重新启动您的应用程序

成功了吗?

【讨论】:

    【解决方案3】:

    这是因为后台所有正在运行的服务器进程。 所以你需要做的就是阻止他们离开终端。

    快速技巧


    Linux 版

    通过在终端上运行来杀死他们:

    pkill -f node
    

    然后重启nodemon。


    FOR 窗口

     1. Go to the task manager
     2. Then search-for node.js server-side javascript
     3. Then right click on it and end-task from the processes. 
    

    然后重新启动服务器。它会正常工作的。

    【讨论】:

    • 在windows上做什么?
    • 对于 Linux:这对我不起作用。我还必须使用 px aux | grep node 并随后使用 kill -9 [PROCCESS_ID] 杀死进程
    【解决方案4】:

    很可能您的节点守护程序仍在运行。

    ps -eaf | grep node

    如果它给出类似的东西

    abhinav 15759 15748 0 19:30 pts/2 00:00:00 sh -c nodemon server.js abhinav 15760 15759 0 19:30 pts/2 00:00:00 node /home/abhinav/Documents/Projects/WebD/Node/smartbrain/smart-brain-api/node_modules/.bin/nodemon server.js abhinav 15812 15801 0 19:31 pts/2 00:00:00 sh -c nodemon server.js

    然后试试:

    killall node

    【讨论】:

      【解决方案5】:

      windows用户,进入任务管理器,找到node.js server-side javascript,结束进程任务。然后用你的命令再试一次。

      【讨论】:

      • 我试图用各种命令杀死终端中正在运行的服务器......不起作用,但这个简单的技巧做到了。谢谢@Jamim Uddin。
      【解决方案6】:

      在终端中使用以下命令终止现有正在运行的端口并重新启动您的服务。

      killall -9 node
      

      或 杀死一个特定的端口而不是全部

      sudo lsof -i :3000 //replace 3000 with your port number
      sudo kill -9 31363 // replace 31363 with your PID
      

      【讨论】:

        【解决方案7】:

        确保您的系统时间不向后或更高。 重置你的系统时间,如果你使用在线数据库,它可以连接到猫鼬服务器。

        【讨论】:

          【解决方案8】:

          如果路径不正确,可能会发生这种情况。

          示例:如果您的 nodemon server.js 路径在 D:\Ex-Track\mern-exercise 中,但如果您没有在 grunt 中使用完整路径 D:\Ex-Track,这可能会导致 nodemon 失败。

          【讨论】:

          • 答案不明确,格式不正确。
          【解决方案9】:

          有时当您忘记获取 .env 文件时会发生这种情况

          【讨论】:

          • 谢谢!我敢肯定,你让我免于再拉几个小时的头发。
          【解决方案10】:

          问题: 应用程序崩溃 - 在启动前等待文件更改...

          可能有不同的原因:

          1.在后台运行节点

          pkill -f node

          在 Mac OS 中工作过,在 Microsoft 中没有工作过。

          2.Server.js 和 package.json 不在同一个文件夹中。检查一下。

          检查 package.json

          "scripts": {
              "test": "echo \"Error: no test specified\" && exit 1",
               "start": "nodemon Server.js"
           }
          

          【讨论】:

            【解决方案11】:

            如果您的 PORT 被先前的节点进程(任务)使用

            运行命令netstat -a -n -o |找到“8080”

            并使用以下命令传递打开的任务

            taskkill /F /PID "1544"

            【讨论】:

              【解决方案12】:

              这通常发生在您对服务器进行更改时,而您的服务器正在运行。相反,当您 git pull 对您的服务器进行更改时。因此,您需要杀死正在运行的服务器的 PID。

              在 windows 上,您需要运行以下命令:

              > **netstat -ano|findstr "PID :3000"** // Replace 3000 with the port number
              > **taskkill /pid 18264 /f** // Replace the 18264 with the pid number you received by running the first command
              

              在 Linux/Unix 系统上,您需要运行以下命令:

              > **lsof -i tcp:3000**
              > **Kill -9 18264** 
              

              【讨论】:

                【解决方案13】:

                !!!对我有用?

                [nodemon] 应用程序崩溃 - 在启动前等待文件更改... 意味着您在保存(nodemon 调试器)工作时准确地更改了文件,请尝试使用历史记录来返回您的更改。只是。如果您的更改无法从历史记录中恢复,请执行以下步骤:

                像这张图片一样在你的根节点模块中删除你的nodemon

                MacBook-Pro:/usr/local/lib$ cd node_modules/
                MacBook-Pro:/usr/local/lib/node_modules$ ls
                expo-cli/         npm/              react-native-cli/
                nodemon/          react-devtools/
                MacBook-Pro:/usr/local/lib/node_modules$ sudo rm -R n
                nodemon/ npm/     
                MacBook-Pro:/usr/local/lib/node_modules$ sudo rm -R nodemon/
                MacBook-Pro:/usr/local/lib/node_modules$ 
                

                然后重新安装 nodmon !!

                【讨论】:

                  【解决方案14】:

                  尝试更改 mongoDB 中的默认用户密码(数据库访问->用户)。这为我解决了这个问题。

                  【讨论】:

                    【解决方案15】:

                    我认为这个错误可能有很多原因(老实说这不是一个描述性很强的错误),在我的情况下是因为“html-pdf”模块有冲突,我删除它,现在错误消失了!。

                    【讨论】:

                      【解决方案16】:
                      "scripts": {
                         "start": "nodemon script.js"
                      }
                      

                      在这里,在nodemon 之后写下你想运行的文件名,就像我在package.json 中写的script.js 一样。

                      希望对你有所帮助?。

                      【讨论】:

                        【解决方案17】:

                        我已经一个多月没用MongoDB了。因此它自动暂停了 MongoDB 云(atlas)中的 MongoDB 集群。我恢复了集群并重新启动了应用程序(同时重新检查了保存在 default.json 中的 mongo 连接 URL)。

                        这对我来说是这样的。

                        【讨论】:

                          【解决方案18】:

                          在 Windows 上杀死 node.js 服务的更简单方法是在命令提示符下运行此命令

                          taskkill /im node.exe /F
                          

                          【讨论】:

                            【解决方案19】:

                            那只是因为您的 mongoURI 配置中的密码不正确,您的用户名是 1 但密码不正确。只需为创建的集群找到正确的密码,然后保存它应该适用于 MERN 堆栈

                            【讨论】:

                            • 请添加更多详细信息以扩展您的答案,例如工作代码或文档引用。
                            【解决方案20】:

                            只需保存“Express route”文件即可重启, 运行过程中可能出现错误(异常或其他)

                            【讨论】:

                              【解决方案21】:

                              可能非常简单 - 如果您在控制台中看到此问题,请向上滚动一下。如果你的代码有问题,它会写出确切的位置。

                              【讨论】:

                              猜你喜欢
                              • 2020-01-19
                              • 2022-01-15
                              • 2018-04-29
                              • 2017-10-15
                              • 2021-09-04
                              • 2021-05-31
                              • 1970-01-01
                              • 1970-01-01
                              • 2022-08-18
                              相关资源
                              最近更新 更多