Running a command with gulp to start Node.js server
Asked Answered
S

4

34

So I am using gulp-exec (https://www.npmjs.com/package/gulp-exec) which after reading some of the documentation it mentions that if I want to just run a command I shouldn't use the plugin and make use of the code i've tried using below.

var    exec = require('child_process').exec;

gulp.task('server', function (cb) {
  exec('start server', function (err, stdout, stderr) {
    .pipe(stdin(['node lib/app.js', 'mongod --dbpath ./data']))
    console.log(stdout);
    console.log(stderr);
    cb(err);
  });
})

I'm trying to get gulp to start my Node.js server and MongoDB. This is what i'm trying to accomplish. In my terminal window, its complaining about my

.pipe

However, I'm new to gulp and I thought that is how you pass through commands/tasks. Any help is appreciated, thank you.

Selway answered 20/1, 2015 at 14:39 Comment(1)
nb: You don't need the gulp-exec module. Stated in the first note: comment on the module page ➝ npmjs.com/package/gulp-execCarman
S
43
gulp.task('server', function (cb) {
  exec('node lib/app.js', function (err, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
    cb(err);
  });
  exec('mongod --dbpath ./data', function (err, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
    cb(err);
  });
})

For future reference and if anyone else comes across this problem.

The above code fixed my problem. So basically, I found out that the above is its own function and therefore, doesn't need to:

.pipe

I thought that this code:

exec('start server', function (err, stdout, stderr) {

was the name of the task I am running however, it is actually what command I will be running. Therefore, I changed this to point to app.js which runs my server and did the same to point to my MongoDB.

EDIT

As @N1mr0d mentioned below with having no server output a better method to run your server would be to use nodemon. You can simply run nodemon server.js like you would run node server.js.

The below code snippet is what I use in my gulp task to run my server now using nodemon :

// start our server and listen for changes
gulp.task('server', function() {
    // configure nodemon
    nodemon({
        // the script to run the app
        script: 'server.js',
        // this listens to changes in any of these files/routes and restarts the application
        watch: ["server.js", "app.js", "routes/", 'public/*', 'public/*/**'],
        ext: 'js'
        // Below i'm using es6 arrow functions but you can remove the arrow and have it a normal .on('restart', function() { // then place your stuff in here }
    }).on('restart', () => {
    gulp.src('server.js')
      // I've added notify, which displays a message on restart. Was more for me to test so you can remove this
      .pipe(notify('Running the start tasks and stuff'));
  });
});

Link to install Nodemon : https://www.npmjs.com/package/gulp-nodemon

Selway answered 20/1, 2015 at 15:8 Comment(13)
How do you stop one of the servers?Osyth
@Osyth above i'm running both my node server and database in on gulp task. If you wanted to stop one and run one you'd have to have two different gulp tasksSelway
@Osyth exec('killall mongod', function (err, stdout, stderr) {Borodino
This starts my server but does not seem to print any of the servers output.Autopilot
I found npmjs.com/package/gulp-nodemon to be a pretty good solution as well with the added benefit of having nodemon integrated into it.Autopilot
@Autopilot yeah I run node server.js which doesn't give you any output unless you hit the port and it still doesn't give you much. I also use the nodemon package now. It auto restarts if any changes have been made which is super handy!Selway
@Autopilot just updated the answer with what I now use with Nodemon. Cheers man!Selway
@SmurfEkko, you can get output using child_process.spawn() with options { stdio: 'inherit' }. It's a only a few lines of code, and you don't need the gulp-exec or gulp-nodemon plugins. Please see https://mcmap.net/q/451516/-gulp-with-nodemon-watch-for-file-changes-quot-app-crashed-waiting-for-file-changes-before-starting-quotMartz
@T.Webster I never knew that! Though I do like the simplicity of gulp-nodemon and what it gives you. But i'll look at using the child_process.spawn() method thanks man!Selway
@SmurfEkko no problem, and you'll find that it's straightforward to start node debugging from a gulp task, too, if you want.Martz
How to install the notify?Santanasantayana
@JeffTian it's an npm module the link: npmjs.com/package/gulp-notify. You'd npm install gulp-notify then add it into your gulpfile like var notify = require('gulp-notify'), then just call it within your function and pass it through some params like i'm doing in the above :)Selway
why console.log() in my app.js not shown on cli?Cabman
S
10

This solution has stdout/stderr shown as they occur and does not use 3rd party libs:

var spawn = require('child_process').spawn;

gulp.task('serve', function() {
  spawn('node', ['lib/app.js'], { stdio: 'inherit' });
});
Sekyere answered 16/9, 2016 at 14:36 Comment(1)
Just remember to kill the child process before restarting it.Northington
C
5

You can also create gulp node server task runner like this:

gulp.task('server', (cb) => {
    exec('node server.js', err => err);
});
Corrupt answered 20/2, 2018 at 5:0 Comment(0)
S
0

If you want your console to output everything that the child process outputs, as well as pass to the child process all environment variables you already have set:

const exec = require('child_process').exec;

function runCommand(command, cb) {
  const child = exec(command, { env: process.env }, function (err) {
    cb(err);
  })
  child.stdout.on('data', (data) => {
    process.stdout.write(data);
  });
  child.stderr.on('data', (data) => {
    process.stdout.write(`Error: [${data}]`);
  });
}

Note that both out and err write to stdout, this is intentional for my case but you can adapt to whatever you need.

Swelter answered 9/1, 2023 at 14:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.