简体   繁体   English

在Rails上运行npm软件包的最佳方法是什么?

[英]What's the best way to run npm packages on Rails?

I'm new to rails and am using React for the front end. 我是Rails的新手,并且正在使用React作为前端。 Ideally I would love to be able to go into my component and at the top, place an import statement like: 理想情况下,我希望能够进入我的组件,并在顶部放置如下的import语句:

import npmpackage from 'npm-package' 从'npm-package'导入npmpackage

It seems I cannot do that, and always get some kind of an error from rails when i try to run the server. 看来我无法做到这一点,并且在尝试运行服务器时总是会从Rails中收到某种错误。 I've heard the rails version that I'm using (5.1.3) allows for use of webpack, es6, and import statements like this. 我听说我正在使用的Rails版本(5.1.3)允许使用webpack,es6和import语句,如下所示。

Can anyone explain to me or maybe link me to a good guide on how this is done? 任何人都可以向我解释,或者可以将我链接到有关此操作的良好指南吗? Google seems to link me to unhelpful guides or outdated ones that use rails 4 or earlier. Google似乎将我链接到使用rails 4或更早版本的无用指南或过时的指南。

this is my application.rb file: 这是我的application.rb文件:

 require_relative 'boot' require 'rails/all' # Require the gems listed in Gemfile, including any gems # you've limited to :test, :development, or :production. Bundler.require(*Rails.groups) module Mediumrails class Application < Rails::Application # Initialize configuration defaults for originally generated Rails version. config.assets.paths << Rails.root.join('node_modules'); config.load_defaults 5.1 # Settings in config/environments/* take precedence over those specified here. # Application configuration should go into files in config/initializers # -- all .rb files in that directory are automatically loaded. end end 

How about (In Rails 5.1+): 怎么样(在Rails 5.1+中):

rails new my-react-rails-app webpack=react

Or add gem 'webpacker' to Gemfile and bundle it. 或将gem 'webpacker'添加到Gemfile并将其捆绑。 And then run ./bin/rails webpacker:install:react And just sit tight to give some time to rails to prepare everything :) 然后运行./bin/rails webpacker:install:react并坐下来给./bin/rails webpacker:install:react一些时间来准备一切:)

Maybe this post could be helpful. 也许这篇文章可能会有所帮助。

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM