Using npm to build and deploy Hugo

After moving to Hugo I wanted an easy way to deploy new posts. Opening an FTP client every time is far too much work!

As I’m already using npm to minify the JS and CSS I decided to use that to handle the deploying too.

The following requires both rsync and curl (as well as npm) to be installed. I’ve tested this on Linux and Mac. It should work on Windows too as long as rsync and curl are installed.

First create the package.json file and put the following in it:

  "name": "",
  "version": "1.0.0",
  "private": true,
  "config" : {
    "sitemap" : "",
    "destination" : ""
  "scripts": {
    "clean": "rimraf ./public",
    "prebuild": "npm run clean",
    "build": "hugo",
    "predeploy": "npm run build",
    "deploy": "rsync -crtvz --progress ./public/ $npm_package_config_destination",
    "ping-sitemap:google": "curl -so /dev/null --fail$npm_package_config_sitemap",
    "ping-sitemap:bing": "curl -so /dev/null --fail$npm_package_config_sitemap",
    "ping-sitemap": "npm run ping-sitemap:google & ping-sitemap:bing"
  "devDependencies": {
    "rimraf": "^2.5.4"

Then change the sitemap and destination config values for your site and that’s it, ready to use!


npm run deploy -- --dry-run
npm run deploy
npm run ping-sitemap

Using gulp

For comparrison, here is the gulp alternative:

npm install --save-dev gulp gulp-rsync rimraf minimist request


const gulp = require('gulp');
const rsync = require('gulp-rsync');
const exec = require('child_process').exec;
const rimraf = require('rimraf');
const request = require('request');
const argv = require('minimist')(process.argv.slice(2));

function httpPing(url) {
  return new Promise((resolve, reject) => {
    request(url, function(err, response) {
      if (err) {
      } else if (response.statusCode !== 200) {
        reject(Error('URL: ' + url + ' had status: ' + response.statusCode));
      } else {

gulp.task('clean', function (cb) {
  rimraf('./public', cb);

gulp.task('build', ['clean'], function (cb) {
  exec('hugo', function (err, stdout, stderr) {
    console.log('\n-------- Hugo output--------\n');


gulp.task('deploy', ['build'], function () {
  // TODO: Change the hostname, username and destination below
  return gulp.src('public/**')
      root: 'public/',
      hostname: '',
      username: 'ssh-user',
      dryrun: !!argv['dry-run'],
      incremental: true,
      progress: true,
      times: true,
      compress: true,
      destination: '/home/'

gulp.task('ping-sitemap', function (cb) {
  // TODO: Change the sitemap URL
  const sitemapUrl = '';
  const sitemapParam = encodeURIComponent(sitemapUrl);
  const googlePing = '' + sitemapParam;
  const bingPing = '' + sitemapParam;

  ]).then(() => cb(), cb);


gulp deploy --dry-run
gulp deploy
gulp ping-sitemap

The gulp version is more extensible but for my needs the NPM one works well!