January 22, 2018Ivan Poiraudeau2 min read
Vagrant offers the possibility to sync files between your host and your VM, a great way to edit your code on your favorite IDE while being able to run it in a VM.
Not all files are worth syncing though - have you ever wished to specifically avoid syncing heavy folders to your host such as your node modules or your error logs?
Let's see how doing this can lead to tripling the speed of your npm install.
On one of my projects, I was hit by a file sync issue between my host and my VM, due to the then recently released Apple APFS filesystem.
To mitigate this issue I needed to find a way to avoid my node modules to be synced from my VM to my host, and was helped by a trick found on Stack Overflow.
The idea here will be to replace in your VM your
node_modules folder with a symbolic link pointing to a folder outside of the synced folder(s) - hence, content of
node_modules won't be synced to your host.
What your host will see will only be the symbolic link, which won't point to an actual folder on your host - this shouldn't cause any issue.
Let's say you have already set up a Vagrant synced folder with NFS, for example thanks to the following line in your
config.vm.synced_folder ".", "/your-project", type: "nfs"
If you have already run
npm install you first need to move the
node_modules outside of your synced folder:
⚠️ Note: all commands from now on are to be run in your VM
$ cd /your-project $ mv node_modules /outside-of-synced-folder
If you haven't, you need to create the folder:
$ mkdir /outside-of-synced-folder/node_modules
node_modules has been moved or created, you can create the symbolic link in your project directory:
$ ln -s /outside-of-synced-folder/node_modules /your-project/node_modules
Then you can run:
$ npm install
Trying this with Sound Redux, a popular open-source React project, we time
npm install on a 2017 MacBook Pro:
It will take 41s if
node_modules is synced to the host, and only 14s if not (a 3x improvement)
With Sentry (the crash reporting platform), we go from 1mn30 to 26s (a 5x improvement)
Finally, on my own project using Angular and an older version of npm, we go from 9mn to 3mn (a 3x improvement).
Lead Developer at Theodo