node.js - WebStorm/grunt 调试运行抛出 EADDRINUSE 错误

标签 node.js gruntjs webstorm

我有一个 Node/Angular 应用程序,是使用 WebStorm IDE 开发的。我可以通过 WebStorm (Shift + F10) 正常运行应用程序。但是,每当我尝试以 Debug模式运行时,我都会收到 EADDRINUSE 错误:

Running "concurrent:default" (concurrent) task
Verifying property concurrent.default exists in config...OK
Files: [no src] -> default
Options: limit=10, logConcurrentOutput
Error: listen EADDRINUSE :::52387

这是我的 gruntfile.js - 就像我说的,WebStorm 构建并运行它很好,直到我尝试在 Debug模式下运行它,它抛出错误:

'use strict';

var fs = require('fs');

module.exports = function(grunt) {
// Unified Watch Object
var watchFiles = {
    serverViews: ['app/views/**/*.*'],
    serverJS: ['gruntfile.js', 'server.js', 'config/**/*.js', 'app/**/*.js', '!app/tests/'],
    clientViews: ['public/modules/**/views/**/*.html'],
    sass: ['public/css/*.scss'],
    clientJS: ['public/js/*.js', 'public/modules/**/*.js'],
    clientCSS: ['public/modules/**/*.css'],
    mochaTests: ['app/tests/**/*.js']
};

// Project Configuration
grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    watch: {
        serverViews: {
            files: watchFiles.serverViews,
            options: {
                livereload: true
            }
        },
        serverJS: {
            files: watchFiles.serverJS,
            tasks: ['jshint'],
            options: {
                livereload: true
            }
        },
        clientViews: {
            files: watchFiles.clientViews,
            options: {
                livereload: true
            }
        },
        clientJS: {
            files: watchFiles.clientJS,
            tasks: ['jshint'],
            options: {
                livereload: true
            }
        },
        clientCSS: {
            files: watchFiles.clientCSS,
            tasks: ['csslint'],
            options: {
                livereload: true
            }
        },
        sass: {
            files: watchFiles.sass,
            tasks: ['sass:dev'],
            options: {
                livereload: true
            }
        },
        mochaTests: {
            files: watchFiles.mochaTests,
            tasks: ['test:server'],
        }
    },
    jshint: {
        all: {
            src: watchFiles.clientJS.concat(watchFiles.serverJS),
            options: {
                jshintrc: true
            }
        }
    },
    csslint: {
        options: {
            csslintrc: '.csslintrc'
        },
        all: {
            src: watchFiles.clientCSS
        }
    },
    uglify: {
        production: {
            options: {
                mangle: false
            },
            files: {
                'public/dist/application.min.js': 'public/dist/application.js'
            }
        }
    },
    cssmin: {
        combine: {
            files: {
                'public/dist/application.min.css': '<%= applicationCSSFiles %>'
            }
        }
    },
    nodemon: {
        dev: {
            script: 'server.js',
            options: {
                nodeArgs: ['--debug'],
                ext: 'js,html',
                watch: watchFiles.serverViews.concat(watchFiles.serverJS)
            }
        }
    },
    'node-inspector': {
        custom: {
            options: {
                'web-port': 1337,
                'web-host': 'localhost',
                'debug-port': 5858,
                'save-live-edit': true,
                'no-preload': true,
                'stack-trace-limit': 50,
                'hidden': []
            }
        }
    },
    concurrent: {
        default: ['nodemon', 'watch'],
        debug: ['nodemon', 'watch', 'node-inspector'],
        options: {
            logConcurrentOutput: true,
            limit: 10
        }
    },
    env: {
        test: {
            NODE_ENV: 'test'
        },
        secure: {
            NODE_ENV: 'secure'
        },
        development: {
            NODE_ENV: 'development'
        }
    },
    mochaTest: {
        src: watchFiles.mochaTests,
        options: {
            reporter: 'spec',
            require: 'server.js'
        }
    },
    karma: {
        unit: {
            configFile: 'karma.conf.js'
        }
    },
    sass: {
      dev: {
        files: {
          'public/css/style.css': 'public/css/*.scss'
        }
      }
    },
    copy: {
        localConfig: {
            src: 'config/env/local.example.js',
            dest: 'config/env/local.js',
            filter: function() {
                return !fs.existsSync('config/env/local.js');
            }
        }
    }
});

// Load NPM tasks
require('load-grunt-tasks')(grunt);

// Making grunt default to force in order not to break the project.
grunt.option('force', true);

// A Task for loading the configuration object
grunt.task.registerTask('loadConfig', 'Task that loads the config into a grunt option.', function() {
    var init = require('./config/init')();
    var config = require('./config/config');

    grunt.config.set('applicationJavaScriptFiles', config.assets.js);
    grunt.config.set('applicationCSSFiles', config.assets.css);
});

//Sass task
grunt.registerTask('default', ['lint', 'sass:dev', 'concurrent:default']);

// Debug task.
grunt.registerTask('debug', ['lint', 'copy:localConfig', 'concurrent:debug']);

// Secure task(s).
grunt.registerTask('secure', ['env:secure', 'lint', 'copy:localConfig', 'concurrent:default']);

// Lint task(s).
grunt.registerTask('lint', ['jshint', 'csslint']);

// Build task(s).
grunt.registerTask('build', ['lint', 'loadConfig', 'ngAnnotate', 'uglify', 'cssmin']);

// Test task.
grunt.registerTask('test', ['copy:localConfig', 'test:server', 'test:client']);
grunt.registerTask('test:server', ['env:test', 'mochaTest']);
grunt.registerTask('test:client', ['env:test', 'karma:unit']);
};

最佳答案

问题是由 Grunt 生成子任务的方式引起的。默认情况下,grunt.util.spawn() 继承主进程参数并使用与主进程相同的选项启动子进程 (--debug-brk=52387),因此子进程在与主进程相同的调试端口上启动,导致 EADDRINUSE。参见 https://github.com/gruntjs/grunt/issues/1420 .

作为解决方法,尝试将 process.execArgv = []; 添加到 Gruntfile.js 的顶部 - 它应该会有所帮助

关于node.js - WebStorm/grunt 调试运行抛出 EADDRINUSE 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37303080/

相关文章:

windows - grunt "Error: Cannot find module ' ../time/convert'(仅在 Windows 上)

javascript - 在构建过程中将代码包装在自定义闭包中

webstorm - 如何防止 node_modules 包中的 TODO 出现在 WebStorm 中?

node.js - 运行 react 脚本测试时为 "Error: spawn git ENOENT"

javascript - Node ,Express - 无法获取路线

node.js - 如何在React-Redux中上传多个文件

javascript - 如何结束异步调用以同步运行?

node.js - 如何使用 grunt 使 tinymce 在开发和 dist 环境中工作?

javascript - 使用 WebStorm 时不显示 .ejs 中的图像

javascript - 如何让 WebStorm 解析功能模块?