Start application with pm2 if starts form gulp
Asked Answered
C

1

7

Hi I was following a tutorial to build an nodejs app. This tutorial is using gulp, to run my app I just do gulp dev to start all things. In my gulpfile I have:

var fs = require('fs')
var gulp = require('gulp')

fs.readdirSync(__dirname+'/gulp').forEach(function(task){
    require('./gulp/' + task)
})

gulp.task('dev', ['watch:css', 'watch:js', 'dev:server'])

The code in ./gulp/server.js

var gulp = require('gulp')
var nodemon = require('gulp-nodemon')

gulp.task('dev:server', function(){
    nodemon({
        script: 'server.js',
        ext:'js',
        ignore: ['ng*', 'gulp*', 'assets*'],
        env: { 'NODE_ENV': require('../config').ENV }
    })
})

Is it possible somehow to use pm2 with this gulp setup, I saw some questions in stackoverflow and google, but cant get anything done. If someone can help me would be great. Thank you in advance.

Chest answered 27/9, 2015 at 12:16 Comment(2)
It should be possible, but we'd like to know the code in ./gulp/(server).jsGastroenteritis
Hi code-jaff, thx I updated my questionChest
G
12

It's available at http://pm2.keymetrics.io/docs/usage/pm2-api/

var gulp = require('gulp');
var pm2 = require('pm2');

gulp.task('dev:server', function () {
    pm2.connect(true, function () {
        pm2.start({
            name: 'server',
            script: 'server.js',
            env: {
                "NODE_ENV": require('../config').ENV
            }
        }, function () {
            console.log('pm2 started');
            pm2.streamLogs('all', 0);
        });
    });
});

Implementation from https://github.com/Unitech/PM2/issues/1525#issuecomment-134236549

Gastroenteritis answered 27/9, 2015 at 14:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.