Introduction to gulp-nodemon
gulp-nodemon
integrates two powerful tools for Node.js development, Gulp and Nodemon, to enhance your development workflow. It keeps your server running, automatically restarting on file changes. Let’s explore its APIs and practical usage with code snippets.
Installing gulp-nodemon
npm install gulp-nodemon --save-dev
Basic Usage
const gulp = require('gulp');
const nodemon = require('gulp-nodemon');
gulp.task('start', () => {
nodemon({
script: 'app.js'
});
});
Watching Multiple Files
Automatically restart your server when multiple files change:
gulp.task('start', () => {
nodemon({
script: 'app.js',
watch: ['app.js', 'routes/*.js']
});
});
Executing Additional Tasks
Run Gulp tasks before restarting the server:
gulp.task('start', () => {
nodemon({
script: 'app.js',
tasks: ['lint']
});
});
Ignoring Specific Files
Ignore specific files or directories to avoid unnecessary restarts:
gulp.task('start', () => {
nodemon({
script: 'app.js',
ignore: ['test/', 'public/']
});
});
Customizing Environment Variables
Set environment variables for different environments:
gulp.task('start', () => {
nodemon({
script: 'app.js',
env: { 'NODE_ENV': 'development' }
});
});
Delay Between Restarts
Add a delay between restarts to avoid rapid successive restarts:
gulp.task('start', () => {
nodemon({
script: 'app.js',
delay: 10
});
});
App Example with Complete Setup
Let’s put it all together in an app example:
const gulp = require('gulp');
const nodemon = require('gulp-nodemon');
gulp.task('lint', () => {
// linting task code here
});
gulp.task('start', () => {
nodemon({
script: 'app.js',
watch: ['app.js', 'routes/*.js'],
tasks: ['lint'],
ignore: ['test/', 'public/'],
env: { 'NODE_ENV': 'development' },
delay: 10
});
});
gulp.task('default', gulp.series('start'));
With the above setup, your development server will restart automatically upon detecting changes in specified files while executing additional tasks, providing a seamless development workflow.
Hash: 8328054146be65242493bac14e44f2e814861483b8e42dc5a7c6581c3722b142