Search code examples
javascriptnode.jstypescriptgulpnodemon

gulp-nodemon watch typescript files


I have 2 directories:

  • app es5 javascript files compiled by gulp task

  • src typescript source files

I want to nodemon make start script from app/ restart on any changes in src/ dir. But it doesn't work - App just started, but nodemon doesn't make any reloads on changes. (Looks like he doesn't watch src/)

This is my gulpfile:

var gulp = require("gulp-help")(require("gulp")),
    ts = require("gulp-typescript"),
    nodemon = require("gulp-nodemon");

/***********************************************************************************
 * Build es5 javascript files from typescript sources
 ***********************************************************************************/
gulp.task("compile", function() {
    var project = ts.createProject({
        "target": "ES5",
        "module": "commonjs",
        "moduleResolution": "node",
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "removeComments": true,
        "noImplicitAny": false
    });
    return gulp.src("./src/**/*.ts")
        .pipe(project())
        .pipe(gulp.dest("./app/"))
});

/***********************************************************************************
 * Call "build" task and start nodemon with watch (autorestart on changes found)
 ***********************************************************************************/
gulp.task("default", ["compile"], function() {
    var stream = nodemon({
        script: "app/",
        watch: "src",
        tasks: ["compile"],
        env: { "DEBUG": "Application,Request,Response" }
    });
    return stream;
});

Solution

  • In the provided code you are missing the starting file for script:

    The Following script: "app/" needs to be specified to an actual file not a directory.

    For example:

    Note: make sure you are using the compiled .js file and not a .ts file for the nodemon server.

    script : 'app.js'
    

    or you can put a path to make sure you are in the correct location.

    script : 'app/app.js
    

    What this does it starts the server using the specified file. If this does not work I recommend looking at your file structure, in specific where the gulpfile.js file is located. Depending on the location you might not be using the correct path.