Rails is such a brilliant framework, it’s got almost all the possible features inbuilt. However, even though it has a lot of support for front end libraries and frameworks, it lacks support for node packages. We need to rely on a third party tool called Browserify, for this.
So I thought of trying it, but it wasn’t as easy as I thought. I failed many times. Sometimes I thought of dropping the idea, but my curiosity never let me down. One fine day I finally integrated it. And this is how I did it,
Create a Rails project (or use one you already have)
As a start, create a rails project of your desired name.
Add the following gems to the Gemfile.
Note: react-rails will be used for integrating and using React components directly in ERB files.
Note: browserify-rails will be used for integrating Browserify into the Rails assets pipeline.
And then run this,
Now everything is set to configure Browserify.
In your config/application.rb add the following.
This is to instruct the Rails app on how to bundle your npm modules and add them to the asset pipeline.
Setting up npm modules
The first step to set up npm in your rails app is of course to initializing it.
npm init
This will ask you a set of configuration questions, once completed it will create a package.json file in your root folder. This package.json is like a Gemfile, but for Node. Read more about it on the npm website .
Now its time to add our core dependencies and devDependencies for node. As we’re going to develop on React, we’ll add it also as a dependency.
Here, the packages I added under devDependencies are used to transform of Javascript into a way that can run on the browser.
Now run the following command to install all the packages to your Rails app.
npm install
You can see that a new folder has been created in the root folder of your Rails app named node_modules, this is where all your npm packages go after installation.
After installing all the required modules, create the following files and folder under your app/assets/javascript.
A File
components.js
A Folder
components/
components.js is the file where you’ll be referring all your major and global components with Browserify’s *require *method. And components is the directory where we’ll place all our custom crafted components.
Add the following to the corresponding files,
For testing we’re be creating a sample component which will be placed inside the components folder and has the following content,
We’re almost there! Now you can define all the components you want, just like this. Lets try using it.
rails g controller home index
This will create a new controller and action home#index so you can view it from app/views/home/index.html.erb.
Now you can render the SampleComponent in the view by adding this to app/views/home/index.html.erb
Then start the Rails server and navigate to http://localhost:3000/home/index .
If you can see something similar to the below screen, there then you’ve installed Browserify! :)
Hope the article helps you fixing browserify and react for your rails application. Please feel free to comment down below if you’ve got any queries.
Also I’ve published the source code on github.com/varun , clone and have your way with it!