spitfire109
spitfire109

Reputation: 10039

How to include NPM Module in Rails using browserify-rails

I have included Browserify-Rails in my project because I want to be able to use NPM Installable code, specifically for React and React-Rails.

I installed the gem and have a package.json file and a node_modules file in my root and everything is working in terms of requiring modules within my assets. But I primarily installed the gem so I can get access to code like this:

https://github.com/stayradiated/react-colorpicker

Following those instructions, the NPM package is successfully showing up in the node_modules directory, but when I try to run the simple code, requiring the module, I get

Error: Cannot find module 'react-colorpicker' from '/Users/me/Sites/my_app/app/assets/javascripts'

Obviously the code is not in assets, but how do I access this code? I am open to all alternate ways of using NPM packages like this in Rails that are not huge hacks as well.

Upvotes: 2

Views: 1429

Answers (1)

jovid78
jovid78

Reputation: 11

I guess you either figured it out or found the solution. But, for the record, this worked for me (in application.js):

window.nameofmodule = require ('nameofmodule')

No need to add a path, not even the .js extension. I needed to include it in window object, but, of course, you don't have to.

Upvotes: 1

Related Questions