Question

I've started playing around with Koa, but so far have been unable to find a decent solution for automatically reloading the application upon code changes.

My understanding is that nodemon is the preferred solution in the Node universe, but I'm getting errors due to the --harmony flag (required by Koa):

$ node_modules/.bin/nodemon /path/to/node-unstable/bin/node app.js
/path/to/node-unstable/bin/node --harmony $@
                     ^^^^^^^
SyntaxError: Unexpected identifier
[nodemon] app crashed - waiting for file changes before starting...
Was it helpful?

Solution

node_modules/.bin/nodemon --harmony-generators app.js should be sufficient

OTHER TIPS

I would like to recommend you "pm2" : http://pm2.keymetrics.io/

pm2 is a process manager. It manages your applications states, so you can start, stop, restart and delete processes.

You can easily install pm2 (generally on your machine) typing: sudo npm install -g pm2

Basically pm2 when see some changes in your code, he restart your process istantly, if your process crash he will be restarted and the error will be logged.. For more information take a look on the documentation page: http://pm2.keymetrics.io/docs/usage/cluster-mode/

Setting the 'execMap' option in this gulp task works for me:

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

gulp.task('serve-dev', function() {
    var options = {
        script: './src/server/app.js',
        execMap: { 
            "js": "node --harmony"
        },
        delayTime: 1,
        env: {
            'PORT': port,
            'NODE_ENV': 'dev'
        },
        watch: ['./src/server/']
    };

    return nodemon(options);
});

Obviously your other options may differ, but I included the whole thing as it annoys me when I'm learning something to only see the bare minimum in an answer.

I've recently finished creating a simple web API using KOA, and as for a reload action after code changes i used babel-watch

The advantage of using it is already stated in the repo:

If you're tired of using babel-node together with nodemon (or similar solution). The reason why the aforementioned setup performs so badly is the startup time of babel-node itself. babel-watch only starts babel in the "master" process where it also starts the file watcher. The transpilation is performed in that process too. On file-watcher events, it spawns a pure node process and passes transpiled code from the parent process together with the source maps. This allows us to avoid loading babel and all its deps every time we restart the JS script/app.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top