Reputation: 26758
So this seems like a simple question but I can't manage to find any links about it.
I am using coffee-rails and have a file app/javascript/packs/post_form.js.coffee
. I am including it in my HTML page via javascript_pack_tag 'post_form'
.
The question is, how do I load a dependent JS file from here? I tried putting inside post_form.js.coffee
:
HelperFunctions = require("lib/helper_functions")
And then in app/javascript/lib/helper_functions.js.coffee
:
module.exports = {
UsefulMethod: -> console.log("I'm helpful!")
}
I'm getting
> Cannot find module 'lib/helper_functions'
My config/webpack/environments.js
file is as follows:
const { environment } = require('@rails/webpacker')
const coffee = require('./loaders/coffee')
const webpack = require('webpack')
environment.plugins.prepend('Provide', new webpack.ProvidePlugin({
$: 'jquery/src/jquery',
jQuery: 'jquery/src/jquery',
jquery: 'jquery',
'window.jQuery': 'jquery',
Popper: ['popper.js', 'default']
}))
environment.loaders.prepend('coffee', coffee)
module.exports = environment
Upvotes: 1
Views: 958
Reputation: 543
You've to require it relatively. Try this:
// app/javascript/packs/post_form.coffee
HelperFunctions = require("../lib/helper_functions");
Upvotes: 3