npm install largely does not work within Vagrant
orblivion opened this issue · 0 comments
When I try to build an app that uses npm install with vagrant-spk, certain node packages have assorted errors of expected files missing during the build process. If I ssh into the VM to inspect the situation, I find that the containing directories are often not there either.
I poked around the Internet and it seems to be a known issue with VirtualBox and npm:
I'm not sure I get the complete picture of what's going on. Supposedly this is due to VirtualBox disabling symlinks for synced folders for security reasons. From direct observation I can ssh into my VM and create a symlink, so I'm not sure exactly what's going on. But my npm is in fact failing.
Proposed Workarounds
Turn symlinks back on with a setting
You can turn the symlinks back on with a setting if you trust the guest VM. I tried it once and it didn't work. I likely did it wrong.
- https://www.speich.net/articles/en/2018/12/24/virtualbox-6-how-to-enable-symlinks-in-a-linux-guest-os/
- https://serverfault.com/questions/501599/vagrant-synced-folders-and-vboxinternal2-sharedfoldersenablesymlinkscreate
Bind Mount (worked for me)
You can apparently work around this with a bind mount:
Now, I'm not sure if I'm getting this suggestion right, but it worked for me in the following way:
- In
setup.sh:- create
/opt/build
- create
- In
build.sh:- copy
/opt/appto/opt/build/app sudo mount --bind /opt/build/app /opt/app- at this point
/apt/appis not synced to your host, but npm will build thinking it's sitting in/opt/app.
- at this point
- do build as normal within
/opt/app- this of course actually builds in
/opt/build/app, but if your generated files ever include an absolute path (as npm packages have) they will show up as/opt/app.
- this of course actually builds in
sudo umount /opt/app- copy
node_modulesand other generated directories from/opt/build/appto their respective places in/opt/app- Now the built node modules are inside the synced folder, and should show up in your host
- copy
At this point it will run. If there's a problem with symlinks, I'm not sure why it does, given that it can't build. I even tried comparing the node_modules directories in /opt/app and /opt/build/app/ and they're the same.
Other
There are others. One was to use NFS to sync them, not sure how secure that is in this context.
Assorted:
- https://www.virtualbox.org/ticket/10085 - General ticket related to symlinks, not npm
- https://stackoverflow.com/questions/24200333/symbolic-links-and-synced-folders-in-vagrant - Another report, not particularly related to npm