Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@vaz
Last active October 31, 2020 12:50
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vaz/4720e18ac2fb8a50e40e3c81a1e69364 to your computer and use it in GitHub Desktop.
Save vaz/4720e18ac2fb8a50e40e3c81a1e69364 to your computer and use it in GitHub Desktop.
Various fixes for file-watching and live-reloading when running servers in Vagrant

Vagrant fixes for file-watching servers

These snippets show how to enable polling for filesystem changes when using

  • nodemon for Node apps (ExpressJS, etc)
  • rails server
  • webpack-dev-server --watch
  • ember server

You can probably look at these examples and Google to find similar solutions for other dev servers.

Explanation

Development servers, transpilers and other build tools often provide a watch command, to watch for source file changes and recompile/reload automatically.

Programs running in a Vagrant VM usually can't detect file changes when a file is saved on the host OS (e.g. most editors). So you need to switch to polling (periodically checking for changes) instead.

(Compared to filesystem events, polling is slower to respond to changes and much more I/O-intensive, so it's not often enabled by default. Also, the issue actually belongs more specifically to VirtualBox and the way it does shared directories. Running Vagrant atop something like VMware may just work.)

Fixes

nodemon

nodemon is a tool for running NodeJS apps and automatically reloading whenever certain files change.

It seems nodemon can be used normally with varying success in Vagrant long bug thread here. If it doesn't reload properly:

  • Try using the -L flag (nodemon -L app.js)

If performance suffers or it takes too long to reload:

  • Try watching only specific directories (nodemon -L --watch src --watch lib)
  • and/or, try ignoring filename patterns you don't want to watch (nodemon -L --ignore 'dist/*.js'). nodemon should already ignore things like node_modules/* by default.

rails server

https://stackoverflow.com/questions/35959490/rails-server-doesnt-see-code-changes-and-reload-files

In config/environments/development.rb:

# uses filesystem events:
# config.file_watcher = ActiveSupport::EventedFileUpdateChecker

# uses polling:
config.file_watcher = ActiveSupport::FileUpdateChecker

FileUpdateChecker will use polling.

webpack-dev-server

Need to add these watch options to enable polling:

{
  aggregateTimeout: 300,
  poll: 1000,
  ignored: /node_modules/
}

Programmatically:

# server.js
var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var config = require('./webpack.config');

new WebpackDevServer(webpack(config), {
    publicPath: config.output.publicPath,
    watchOptions: {
      aggregateTimeout: 300,
      poll: 1000,
      ignored: /node_modules/
    }
  })
  .listen(3000, '0.0.0.0', function (err, result) {
    if (err) {
      console.log(err);
    }

    console.log('Running at http://0.0.0.0:3000');
  });

Or command-line:

node_modules/.bin/webpack-dev-server --host 0.0.0.0 --watch --watch-aggregate-timeout 300 --watch-poll 1000

Note: Not sure if --watch-ignore is a thing or if it works, or how to ignore a filename pattern on the command-line. But ignoring the regex pattern /node_modules/ improves performance a lot (only works for Webpack 2.x).

ember server

In .ember-cli, add these properties:

"liveReload": true,
"watcher": "polling"

or invoke the serve command using the polling watcher:

ember serve --watcher polling
@neenus
Copy link

neenus commented Oct 31, 2020

Exactly what I was looking for thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment