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.
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.
Thank you so much, this solution works for me perfectly.
Glad it worked for you! I’m hoping Vagrant (or maybe Virtualbox?) will eventually put in a more permanent fix.
We are all waiting for this issue fix! :/
this worked for my Homestead server npm install so I could have gulp with Laravel.
(problem was Windows short paths).
Great article, but I still get the error msg after running npm install –no-bin-links
What is your error message Tan?
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.
Glad I could help!
This was a life saver for sure. Thank you!!!
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?
Hi Can, Sorry I can’t be of much help as I’m still running 4.3.X.
This commit ( https://github.com/mitchellh/vagrant/commit/7a94eba0b4d6de02e94832943e0fdee8d86066bd ) looks like it ported this fix to VirtualBox 5. It’s possible this is already fixed if you have the latest versions. Let us know how you get on.
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.
Thank you saved my life and my work
‘\\?\’ + folder[:hostpath].gsub(/[/\]/,’\’)]
I’m getting syntax error, looks like ] at the end of expression should be removed
Same here, and if I remove the ] from the end ‘vagrant up’ fails on me
VirtualBox-4.3.30-101610-Win and this post are the key. Ty
This fix doesn’t work on Windows 10 + Virtualbox 5.0
I just updated the post, if you’re using VB 4.3.x that should point you in the right direction
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
I made npm install working in Vagrant on Windows 10 in following steps:
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
Any new information about Homestead on Windows 10 with VirtualBox 5.x ?
I haven’t made the time to try again since my last update. Have you tried it yet?
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.
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
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…
For the moment I installed what I need into Windows (was easier/quicker than setting up dual boot). Anxiously awaiting a real fix here…
I’m using Laragon. It’s pretty good for Laravel development and npm works, but I can’t use Envoy :(
This worked great! Thank you! (side note: I edited the version_5_0.rb file, then it worked)
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.
You can try Long Path Tool, it helped me when I encounter the same issues.
You can also work around this by enabling symlinks as you did above, and enabling the file ACLs on the Windows side (in an administrator prompt) and then rebooting and using something like sympm.
See link for the background, and I’ll have to make a post sometime about using sympm which is freaking awesome. http://blog.prolificinteractive.com/2015/01/21/getting-vagrant-nodejs-windows-play-well-together/