I can’t believe the number of hours I’ve burned trying to figure this out. The gist of this story is that npm install in a vagrant virtual environment sometimes makes some REALLY long paths and it makes Windows freak out with EPERM, EACCESS, and UNKNOWN symlink errors. The solution is to edit a Vagrant file and wait until a proper fix is committed into the official Vagrant repo.

Let me back up for a minute for a little background…

I’ve been using Laravel Homestead (an official Vagrant box) since last summer when I dumped WAMP because of one too many weird quirks. Homestead is made primarily for Laravel development, but I also successfully used it for WordPress development since it’s a nice, well-built, all around LEMP stack.

I’m not exactly sure when it all started, but I began having trouble running the npm install command. Sometimes running npm cache clean would fix it, maybe run as sudo, and sometimes running it from within the windows environment instead of the vagrant environment (or vice versa) would allow it to run. Running it from windows was probably the dumbest workaround because I specifically didn’t want all that stuff (*EMP, Node, etc) in my windows environment to begin with! But eventually it just refused to work on multiple vagrant dev sites no matter what I did so I gave up and decided to destroy the vagrant box and start all over.

Well, that didn’t fix it either and probably made it even worse. Since it was a new VM I decided to fiddle with several npm versions and several node versions. I updated vagrant, virtualbox, the Homestead box configuration, and anything else I could think of. I even decided to try a different Vagrant box configuration and tried VVV (which is really nice, btw). Turning off backups and anti-virus had no effect either. Nothing worked.

Screenshot npm install errors
Screenshot npm install errors

The error…

Here is a sample of one of the many (MANY) error messages that came up.

Error: EPERM, open '/srv/www/harvsworld/htdocs/wp-content/themes/harvsworld/node_modules/gulp-imagemin/node_modules/imagemin/node_modules/imagemin-optipng/node_modules/optipng-bin/node_modules/bin-wrapper/node_modules/download-status/node_modules/lpad-align/package.json'

The EPERM error implies a permission issue and npm suggests you run as sudo. Here’s a hint… It doesn’t work.

Off to google I go…

The trip down the rabbit hole that eventually led to my solution started by finding this link to a forum post titled Cannot install Elixir on Homestead on the excellent laracasts.com video learning site (focused on Laravel). This is where I figured out the error is probably due to the length of paths being too long for windows (see the length of that path in the error above). At the bottom of the forum thread is a link to the real issue on the Vagrant github: support long paths on windows hosts, by rebinding the share to a UNC path.

The fix…

Scroll down through the thread until you get to the fix by chernetsov0 and celtric. Your installation may vary, but this worked for me. In the file: C:\HashiCorp\Vagrant\embedded\gems\gems\vagrant-1.7.2\plugins\providers\virtualbox\driver\version_4_3.rb between lines 495-510. Replace the line folder[:hostpath] with '\\\\?\\' + folder[:hostpath].gsub(/[\/\\]/,'\\')]. The explanation of what it’s doing is in the github link. MAKE A BACKUP FIRST :)

UPDATE: Looks like this was changed in 1.7.3+.

Here is the filepath for me:

C:\HashiCorp\Vagrant\embedded\gems\gems\vagrant-1.7.4\plugins\providers\virtualbox\driver

And the new code looks like this:

def share_folders(folders)
  folders.each do |folder|
	hostpath = folder[:hostpath]
	args = ["--name",
	  folder[:name],
	  "--hostpath",
	  hostpath]
	args << "--transient" if folder.key?(:transient) && folder[:transient]

	# Enable symlinks on the shared folder
	execute("setextradata", @uuid, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/#{folder[:name]}", "1")

	# Add the shared folder
	execute("sharedfolder", "add", @uuid, *args)
  end
end

Which I replace with:

def share_folders(folders)
  folders.each do |folder|
	hostpath = '\\\\?\\' + folder[:hostpath].gsub(/[\/\\]/,'\\')
	args = ["--name",
	  folder[:name],
	  "--hostpath",
	  hostpath]
	args << "--transient" if folder.key?(:transient) && folder[:transient]

	# Enable symlinks on the shared folder
	execute("setextradata", @uuid, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/#{folder[:name]}", "1")

	# Add the shared folder
	execute("sharedfolder", "add", @uuid, *args)
  end
end

This fix has been removed though in 1.7.4 going forward for both 4.3.x and 5.x until further testing. Digging through Github issue #5933 it appears that VirtualBox 5 doesn’t like the UNC file paths as it was implemented here and this was removed until something else can be figured out.

As I write this update on August 29th, only VirtualBox 5 works with Windows 10 (which I just upgraded to) and that means I cannot use my Vagrant boxes. And from the comments it appears I’m not alone…

Well almost…

That fix got me pretty far, but then just when I thought I was in the clear I ran into *this* error:

npm ERR! UNKNOWN, symlink '../strip-ansi/cli.js'

The way I got around that was with npm install --no-bin-links, a command I got from the first link at laracasts and this link at askubuntu.com which tells npm to not create any symlinks during installation.

And voilà! All fixed up.

Parting thoughts… the lengths of some of these paths that npm (and the various dependencies) creates borders on the ridiculous. More importantly, the paths it makes shouldn’t have any influence on the host system since that’s the reason we use these virtual environments in the first place.

32 responses to “How to fix ‘npm install’ errors on vagrant on windows because the paths are too long

  1. Glad it worked for you! I’m hoping Vagrant (or maybe Virtualbox?) will eventually put in a more permanent fix.

  2. this worked for my Homestead server npm install so I could have gulp with Laravel.
    (problem was Windows short paths).

  3. Great article, but I still get the error msg after running npm install –no-bin-links

  4. Thanks, this issue has been bugging me for hours. Even thought there’s lots of information out there about the issue this is first resource that I’ve found that made the fix so easy to implement. I appreciate you taking the time to post this fix. Your pain is my gain.

  5. The fix doesn’t work on VirtualBox 5 and I’m having the same issues. When I change the line the vagrant stops working all together. Any ideas?

  6. Thanks for the reply. I already had the VirtualBox 5 but the problem is there. I just couldn’t figure out so I installed Ubuntu to half of my partition and use them there. However now I have more problems with the other stuff :) so hoping there will be a fix for Windows version.

  7. ‘\\?\’ + folder[:hostpath].gsub(/[/\]/,’\’)]

    I’m getting syntax error, looks like ] at the end of expression should be removed

  8. I just updated the post, if you’re using VB 4.3.x that should point you in the right direction

  9. I’ve been struggling with Windows 10 & Virtualbox 5 since last week. Like @cancelik:disqus I’m about *this* close to just running a dual boot. Wondering if anyone has tried other hosts such as VMWare or Hyper-V

  10. Long Path Tool is a best software to solve this issue. 5++ Rating. Long Path Tool is a best software

    it is a free software. You can Try it

  11. I’ve been running npm install on the windows side (not inside the vagrant VM) which seems to work for now until there is a real fix.

  12. Yes, but I failed on all my attempts :( Laravel development on windows is really annoying (npm problems, no envoyer, etc. + hours of troubleshooting). I think about putting a second drive in my PC with Ubuntu or OSX (Hackintosh) :D

  13. Agree. This is what I’m currently doing, but I don’t like junking up my desktop with even more software installed. I like having everything contained in the VM. Can’t wait for a fix…

  14. For the moment I installed what I need into Windows (was easier/quicker than setting up dual boot). Anxiously awaiting a real fix here…

  15. I’m using Laragon. It’s pretty good for Laravel development and npm works, but I can’t use Envoy :(

  16. This worked great! Thank you! (side note: I edited the version_5_0.rb file, then it worked)

  17. FWIW, I just tried this with all the most recent versions of everything. Nothing worked, and I spent many hours on it and can’t put anymore time in.

    Not limited to this, developing on Windows has been nothing short of a frustrating experience at times. In the near-future I will be getting a Mac. Until then, I’ve resorted to using webpack as a way of avoiding laravel-elixir.

    Thanks for a great post nonetheless. It was very helpful.

Comments are closed.