Safwan S M
Safwan S M

Reputation: 123

Adding external .js files to a new Rails 6 web app

I have created a new rails 6 app. in rails 6, javascript folder is outside assets. I have copied all the css and js files from an existing application to this new one and application.css,application.js are updated. application.css working fine but application.js is not working. while compiling(bin/webpack) webpack it is showing error like this =>

ERROR in ./app/javascript/packs/application.js Module not found: Error: Can't resolve 'file_name' in >'/home/app/javascript/packs' @ ./app/javascript/packs/application.js.

in application.js i have added those .js files with require('file_name') syntax. and i pasted all the .js files on app/javascript/packs folder. please help me to solve this issue.

Upvotes: 3

Views: 3625

Answers (1)

Jonathan Bennett
Jonathan Bennett

Reputation: 1065

The app/javascript/packs folder is special with Webpacker. It should only have the javascript files that "boot up" your page. This would typically be application.js if you were loading everything from a single script, but you could also have something like calendar.js if you had a special calendar page/widget.

All other code would typically be in the app/javascript/src folder. In your app/javascript/packs/application.js file, you could then require('src/file_name') to require the file at app/javascript/src/file_name.js

Upvotes: 4

Related Questions