Installing ruby gems in a npm project

I have a node.js project that depends on a ruby gem. Is it possible somehow to create an installation process that installs the gem dependencies in an easy way? Ideally, when I do

npm install

to install the npm dependencies, it would be great if the required ruby gems were installed as well.

Is there some kind of bridge between the two? If not, has anyone investigated this and found a suggested best practice or work around in these situations?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Theoretically, npm-scripts provides the facilities to run scripts during npm install. You could for example add these lines to your package.json:

{ "scripts" :
  { "preinstall" : "/usr/bin/env gem install some_gem_name" }

Of course, you may want to add a more complex script that handles the case where Ruby and/or Rubygems are not installed, the Gem installation fails etc. etc. Installing dependencies can get arbitrarily complex, which is why many package developers (in any given language) often just assume that the required dependencies are already up and running on the target system. Finally, the npm-scripts documentation states that



The only valid use of install or preinstall scripts is for compilation which must be done on the target architecture.

All in all, I suggest that you instead focus your energy on adding proper installation instructions to your Readme.

Solution 2

If you have multiple gems you need for executing a ruby script in your node.js application, you can make a Gemfile and then add this preinstall/postinstall script to your package.json.

"scripts": {
    "postinstall": "/usr/bin/env bundle install"

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply