How to require custom JS files in Rails 6

Severin picture Severin · May 21, 2019 · Viewed 18.4k times · Source

I'm currently trying Rails 6.0.0.rc1 which seems to have moved the default javascript folder from app/assets/javascript to app/javascript. The application.js file is now located in app/javascript/packs. Now, I want to add a couple of js files, but for some reason they don't get imported and I can't find any documentation on how this can be done in Rails 6. I tried a couple of things:

  1. Create a new folder custom_js under app/javascript/packs, putting all my js files there and then add a require "custom_js" to application.js.

  2. Copy all my js files under app/javascript/channels (which should be included by default, since application.js has require("channels")).

  3. Adding require_tree . to application.js, which was the previous approach.

How can I load my own js files inside a Rails 6 application?

Answer

Oliver Curting picture Oliver Curting · Jun 10, 2019

Get better-organized code and avoid multiple javascript_pack_tags in your application.html.erb file with this approach:

  1. Add your custom example.js javascript file to app/javascript/packs.
  2. Add require("packs/example") to your application.js file.

I would have liked to add a comment to Asim Hashmi's correct answer. But I don't have enough reputation, so I'll add my suggestion as an answer instead.

It isn't necessary to include the ".js" extension inside of the require.