2

I am using Rails 6 in a VirtualBox virtual machine through Vagrant.
Vagrant mounts shared folder (with project) from the host system (Windows) to the Ubuntu 20LTS virtual machine.
And it happens that in my case symlinks don't work in this shared folder.
(There is a key with which the symlinks should work, but it did not happen)

And here's the problem.
When yarn installs packages in node_modules it creates symlinks as well.
And then I get an error because the symlinks don't work:
error An unexpected error occurred: "EPROTO: protocol error, symlink

Ok, I thought that folder node_modules can be moved outside of the project and the shared folder in the virtual machine, where the symlinks will work.
And indeed, you can create a .yarnrc file in the project folder with the contents:
"--modules-folder" "/home/user/custom_path/node_modules"

Now running yarn add or rake webpacker:install will install all packages in the specified directory.
It would seem that the problem is solved.

However, Webpacker doesn't know anything about .yarnrc and keeps looking for it in the root of the project.
And if for example you call rake webpacker:info:

Ruby: ruby 2.6.6p146 (2020-03-31 revision 67876) [x86_64-linux]
Rails: 6.1.4.1
Webpacker: 5.4.3
Node: v16.13.0
Yarn: 1.22.17

@rails/webpacker: 
project@0.1.0 /home/vagrant/data/shared/project
└── (empty)

Is bin/webpack present?: true
Is bin/webpack-dev-server present?: true
Is bin/yarn present?: true

empty - It doesn't see those @rails/webpacker.
However, its in custom node_modules.
I tried writing in config/webpacker.yml
additional_paths: ["/home/user/custom_path/node_modules"]

However, this doesn't have the desired effect.
And at the compile stage I get an error:
Error: Cannot find module '@rails/webpacker'

package.json is in the root of the project and this and other packages are prescribed there.

Question: how to correctly specify the path to node_modules to make everything work?

Yes it's all in the context of development/test environment for rails.

2 Answers2

0

What seems to work for me:

  • Make sure the folder is still called node_modules, wherever it is.

  • Make sure it is in a parent folder of your Rails project

    • Webpack seems to keep stepping up a folder looking for node_modules
  • Tell Webpacker where to find the .bin folder with WEBPACKER_NODE_MODULES_BIN_PATH env variable

    • Webpacker tries to use yarn bin but that doesn't respect the --modules-folder setting in .yarnrc
    • Use the environmental variable to override: export WEBPACKER_NODE_MODULES_BIN_PATH="/node_modules/.bin"
TomG
  • 456
  • 3
  • 11
0

At that time, I forgot to write the answer, because the solution was found over time. For some reason most of the manuals omit one detail (e.g. https://stackoverflow.com/a/50170000/6241373): that by default Windows symlinks work for administrators. You should have allowed to create symlinks for the current user via Group Policies in Windows https://superuser.com/a/105381. Simply add your user. After that the symlinks work fine in Vagrant's shared folders.

Webpack seems to keep stepping up a folder looking for node_modules

It's true.