6

I'm new to Rails and trying to use some Javascript, but everywhere I go I see application.js mentioned, but it's nowhere to be found in my directories.

I followed the official guide and created a blog. But there is no javascript folder or application.js file under app/assets like I'm expecting.

I'm using Rails 7. During installation, I had errors with the tzinfo-data gem, but fixed it later.

Image of the newly created blog assets folder

1
  • I'm actually having the exact same issue. Just installed a fresh ruby rails etc. No JS folders after rails new myapp --css tailwind --database=postgres Commented May 14, 2022 at 22:19

3 Answers 3

6

Run this command. rails importmap:install.This should solve the problem.

Sign up to request clarification or add additional context in comments.

2 Comments

This command doesn't work for me, either during the initial rails new app-name instance or if I try the standalone command of rails importmap:install. Both times I get the message "rails aborted! Don't know how to build task 'importmap:install' (See the list of available tasks with rails --tasks)". Do you know how to get past this? I'm considering creating a new StackOverflow question.
doesn't this delete the contents of application.js?
4

Make sure your preferred javascript handling gem is installed successfully. Each gem's install task will add app/javascript/application.js.


importmap-rails is installed by default in rails 7. It adds an empty application.js.
Install command: bin/rails importmap:install
https://github.com/rails/importmap-rails/blob/v1.1.0/lib/install/install.rb#L11


jsbundling-rails
Install command: bin/rails javascript:install:[esbuild|rollup|webpack]
https://github.com/rails/jsbundling-rails/blob/v1.0.2/lib/install/install.rb#L24


shakapacker (aka webpacker)
Install command: bin/rails webpacker:install
https://github.com/shakacode/shakapacker/blob/v6.4.1/lib/install/template.rb#L11


You should see this bit when running rails new:

...
         run  bundle binstubs bundler
       rails  importmap:install
Add Importmap include tags in application layout
      insert  app/views/layouts/application.html.erb
Create application.js module as entrypoint
      create  app/javascript/application.js          # <= this one
...

Comments

0

This is what I did. add gem Webpcker to Gem file ran bundle install ran webpacker:install The javascript folder was added under assets. Rails 7

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.