如何在 Rails 中使用 npm 包?

编程入门 行业动态 更新时间:2024-10-28 11:34:36
本文介绍了如何在 Rails 中使用 npm 包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在我的 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 包?

    本文发布于:2023-10-29 02:43:55,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1538494.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:如何在   Rails   npm

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!