Rails 6 & Webpacker 5 set custom path for node_modules - ruby-on-rails

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.

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"

Related

Module build failed: Error: spawn bin/rails ENOENT

After adding the ERB loader and adding the .erb file extension to my application pack (with webpacker), I am getting the following error:
ERROR in ./app/webpack/packs/application.js.erb
Module build failed: Error: spawn bin/rails ENOENT
at _errnoException (util.js:1024:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:190:19)
at onErrorNT (internal/child_process.js:372:16)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
# multi (webpack)-dev-server/client?http://localhost:3035 ./app/webpack/packs/application.js.erb
it is also happening with the sample hello_erb.js.erb pack.
Here is an example app that reproduces the problem: https://github.com/jonhue/test_app
You may be missing generated files
If you are missing the bin/rails executable maybe you have cloned from a repository that followed the steps on this issue
Your enviroment should be good to go if you have rails installed correctly and then run:
Rails < 5
$ bundle exec rake rails:update:bin
Rails >= 5
$ rails app:update:bin
You may have issues with webpack installation and/or configuration
If those files on bin\ exists, you may have ran into problems when installing and/or configuring webpack.
Try creating a project installing everything following the documentation, commiting it with a versioning system like GitHub, copy/paste your project configuration and git diff it, I bet you will find differences between them.
You are using Windows or some enviroment that is problematic
If that is the case there is already a discussion to fix it in an open issue in webpacker github. But you could try stuff like using webpacker directly from github to get latest fixes, downgrading to other versions to see if the result is different, or use webpack from source and change the file install.rake that makes you call ./bin/rails app:template... to just rails app:template....
The ERB runner configuration contains a setting that must be changed for it to work on Windows. Open config\webpack\loaders\erb.js and replace this line:
runner: "bin/rails runner"
with this line:
runner: "ruby bin\\rails runner

Webpacker creates huge node_modules

I created a new project with Rails 5.1 and I want to use webpacker to manage my react dependencies. The problem is after installing webpacker it creates a huge 130Mb+ dir node_modules with every possible node_package. This does not make sense as default behavior. How can I configure Webpacker to only keep the packages I'm actually using. I have searched for this issue in every way I could and I did not see any answers that made sense
Install what you need via yarn add, they will be listed in your package.json. Once you run assets:precompile (which will also run webpacker:compile) or just webpacker:compile, the public folder will be populated the compiled assets you are going to need to run the app.
Like Tamer says, node_modules should be in your .gitignore file. In dev, you will see all the node modules due to existing dependencies but that doesn't mean they are going to be used.
In summary, don't worry and rely on yarn.

What is the proper way to serve frontend dependencies managed by Yarn?

With bower, we were able to specify the dependency folder location through .bowerrc, but there doesn't seem to be a way to do this with Yarn. Yarn appears to force the node_modules folder to be at the same level as package.json.
Say I keep my web application's static files in app/resources/static/. I'd like to serve some frontend assets out of here; which are managed by Yarn. I see two ways of doing this, but don't like either one:
1: Init the Yarn package in app/resources/static/. This works, but it also means that package.json will also be visible. No beuno.
2: Init Yarn package in app/, and symlink app/node_modules to app/resources/static. This works, but I'd rather not have this extra step for version control reasons.
What's the best way of doing this? Thanks in advance.
UPDATE: Found a near-solution in this thread: How to change Yarn default packages directory?
You can add --install.modules-folder "./resources" to a .yarnrc file
This works on yarn install, but yarn add packagename will still write the dependency to ./node_modules.

Ruby on Rails - bin files shebang causing issues (Mac)

So for the files in the /bin directory of a normal rails installation (bin/rails, bin/rake, bin/bundle), the shebang at the top of the file is:
#!/usr/bin/env ruby.exe
But when I run bin/rails, for example, I get the error:
env: ruby.exe: No such file or directory
When I remove the .exe from the end of the shebang, everything works fine. But I was just curious:
Why is this necessary
How to deal with versioning, since the other developer working on this doesn't need to remove the .exe. It's suggested to keep the bin folder in the repo, so I'd prefer to just get the .exe version working if anything.
There are no .exe files on Mac OS X or on Linux. On those platforms the Ruby executable is just called ruby.
For compatibility to those operating systems, your first line should look like this:
#!/usr/bin/env ruby

SASS won't build in Sublime Text 2 [Errno 2] No such File or Directory

I've scoured all other posts on this subject to no avail.
I've created a .scss file, installed sass, compass and I'm on OSX so I have ruby installed, I did install the latest version about a year ago however.
When I cmd + b to build my .scss I get this error:
Errno 2] No such file or directory
[cmd: [u'sass', u'--update', u'/Users/administrator/Desktop/style.scss:/Users/administrator/Desktop/style.css', u'--stop-on-error', u'--no-cache']]
[dir: /Users/administrator/Desktop]
[path: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin]
[Finished]
Here is what is in my Ruby.sublime-build file:
{
"cmd": ["ruby", "$file"],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.ruby",
}
The reason the build command isn't working is because Sublime can't find your sass executable. If you open Terminal and type which sass at the prompt, it'll most likely give you a location along the lines of /Users/username/.rvm/gems/ruby-1.9.3-p385/bin/sass - it's location on my system. As you can see right in the output you posted, Sublime is only looking in a few directories for it, unfortunately it's not in any of the directories being searched.
There are two ways around this. The first is to modify your SASS .sublime-build file (Ruby.sublime-build has nothing to do with this) to point to the correct location of sass. The second is to run
sudo ln -s /path/to/sass /usr/local/bin/sass
at the command prompt (obviously substituting the correct path for your system) to create a symlink to sass in /usr/local/bin, which is one of the directories Sublime is searching. Either one will do fine, although if you ever update your ruby version and the sass gem, you'll need to update either the build file or the symlink.

Resources