Installing ruby gems in a npm project

2019-04-06 15:13发布

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?

标签: ruby node.js npm
2条回答
Luminary・发光体
2楼-- · 2019-04-06 15:53

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

INSTALL SCRIPTS ARE AN ANTIPATTERN

and

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.

查看更多
聊天终结者
3楼-- · 2019-04-06 16:01

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"
  },
查看更多
登录 后发表回答