Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use npm packages in rails?

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.

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)?

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.

Thanks!

like image 958
Novice Avatar asked Apr 13 '16 15:04

Novice


People also ask

How do I connect NPM packages?

Example: Let the local-dir is the local directory and project-dir is the project directory and local_module is the local module package you want to install, first go to the local-dir and type npm link and next go to the project directory and type npm link <local_module> this will link your local module to your project.

What is NPM in Ruby on Rails?

NPM is a package manager for Node based environments. NPM manages dependencies and store its data in file package. json .

Which is better yarn or NPM?

As previously stated, Yarn installs dependency packages in parallel, whereas NPM installs them sequentially. As a result, Yarn outperforms NPM when installing bigger files. Both tools can save dependent files to the offline cache.

What is package JSON rails?

A package. json is a JSON file that exists at the root of a Javascript/Node project. It holds metadata relevant to the project and it is used for managing the project's dependencies, scripts, version and a whole lot more.


2 Answers

To include npm packages in a rails project using the asset pipeline, do the following:

  1. Initialise your package.json: npm init

  2. 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

  1. 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?
  1. Install your package: npm install YourPackage

  2. Link to your package from app/assets/javascripts/application.js:

//= require /Path/To/YourPackage
like image 162
Gerard Simpson Avatar answered Sep 21 '22 20:09

Gerard Simpson


Rails 5.1 supports including npm packages using Yarn.

For example, let’s say we want to use the moment.js library. We need first of all install the library using Yarn:

yarn add moment

We can see that package.json was updated:

{
  "name": "yarn_test",
  "private": true,
  "dependencies": {
    "moment": "^2.18.1"
  }
}

And finally we need to include the new package to application.js:

//= require moment/moment

See Rails 5.1 and forward - Part 1: Yarn on Rails

like image 21
Pavel Chuchuva Avatar answered Sep 21 '22 20:09

Pavel Chuchuva