簡體   English   中英

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

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

我是Rails的新手,並且正在使用React作為前端。 理想情況下,我希望能夠進入我的組件,並在頂部放置如下的import語句:

從'npm-package'導入npmpackage

看來我無法做到這一點,並且在嘗試運行服務器時總是會從Rails中收到某種錯誤。 我聽說我正在使用的Rails版本(5.1.3)允許使用webpack,es6和import語句,如下所示。

任何人都可以向我解釋,或者可以將我鏈接到有關此操作的良好指南嗎? Google似乎將我鏈接到使用rails 4或更早版本的無用指南或過時的指南。

這是我的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 

怎么樣(在Rails 5.1+中):

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

或將gem 'webpacker'添加到Gemfile並將其捆綁。 然后運行./bin/rails webpacker:install:react並坐下來給./bin/rails webpacker:install:react一些時間來准備一切:)

也許這篇文章可能會有所幫助。

干杯!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM