Auto-starting Node.js application on Vagrant when there are changes to code

前端 未结 2 362
余生分开走
余生分开走 2021-01-03 12:23

I\'m running a Node.js application on a Vagrant box and want the Node.js application to restart when anything changes on the host machine.

But I couldn\'t use nodemo

相关标签:
2条回答
  • 2021-01-03 12:56

    There seem to be other tools you can try on the guest machine as alternatives to nodemon, but watching for file changes will be the fastest on the host machine.

    Here's a workaround that has worked for me:

    This gist: Use a watcher on the host machine and issue an SSH command to the guest machine to restart the application.

    Here's a recipe (you can use any other tools you like):

    First, grab pm2:

    npm install -g pm2
    

    And launch your Node.js application via pm2:

    pm2 app/server.js
    

    Then grab Gulp:

    npm install -g gulp-cli
    npm install --save-dev gulp
    

    And gulp-shell:

    npm install --save-dev gulp-shell
    

    Then create a gulpfile.js:

    var gulp = require('gulp');
    var shell = require('gulp-shell');
    
    gulp.task('server:watch', function () {
      gulp.watch('app/**/*.js', [ 'server:restart' ]);
    });
    
    gulp.task('server:restart', shell.task([ 'vagrant ssh -- pm2 restart server' ]));
    

    You're done. To start watching the files and automatically start the server on file changes:

    gulp server:watch
    
    0 讨论(0)
  • 2021-01-03 13:18

    Ates' answer provides quite a bit of bloat in my opinion (pm2, Gulp, gulp-shell and an excessive gulpfile?!). A great alternative to nodemon is a similar lib called node-dev. It's also mentioned under nodemon's issues with Vagrant (which you skimmed through, I imagine).

    Basic usage:

    node-dev --no-deps app.js
    

    All the best :)

    0 讨论(0)
提交回复
热议问题