如何在 Rails 中使用 npm 包? [英] How to use npm packages in rails?
问题描述
我正在尝试在我的 Ruby on Rails 应用程序中使用 Ace 编辑器,其中大部分组成为 React 组件的视图.我正在使用 react-rails gem 并且我根本没有使用通量.
I'm trying to use the Ace editor in my Ruby on Rails app, with majority of the view composed as React components. I'm using the react-rails gem and I'm not using flux at all.
我找到了这个 react-ace 包,但我必须使用 npm 来安装它.我已经能够让 bower 组件与 bower-rails gem 一起工作,但从来没有让 npm 包工作.有没有办法仅通过资产管道(通过供应商)来使用它?
I found this react-ace package, but I have to use npm to install it. I've been able to get bower components working with bower-rails gem but never got npm packages to work. Is there a way to use this just through the asset pipeline (through vendor)?
顺便说一下,我没有使用 browserify 或 ES6,所以我什至没有 import
.到目前为止,我一直在通过资产管道做所有事情.
By the way, I'm not using browserify or ES6 so I don't even have import
. I've been doing everything through the asset pipeline so far.
谢谢!
推荐答案
要使用资产管道在 rails 项目中包含 npm 包,请执行以下操作:
To include npm packages in a rails project using the asset pipeline, do the following:
初始化你的 package.json:
npm init
将 node_modules
添加到您的资产路径:
Add node_modules
to your asset path:
# config/application.rb
module YourApp
class Application < Rails::Application
config.assets.paths << Rails.root.join('node_modules')
end
end
- 通过添加初始化程序确保
npm install
在启动时运行:
- Make sure
npm install
runs on startup by adding an initializer:
# config/initializers/npm.rb
system 'npm install' if Rails.env.development? || Rails.env.test?
安装你的包:
npm install YourPackage
从 app/assets/javascripts/application.js
链接到您的包:
//= require /Path/To/YourPackage
这篇关于如何在 Rails 中使用 npm 包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!