# Using CoffeeScript 2 with Stimulus autoloader?

**URL:** <https://discuss.hotwired.dev/t/using-coffeescript-2-with-stimulus-autoloader/1604>\
**Category:** General\
**Created:** [December 23, 2020, 7:10pm UTC](https://discuss.hotwired.dev/t/using-coffeescript-2-with-stimulus-autoloader/1604 "2020-12-23T19:10:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![buhrmi](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/buhrmi/32/1144_2.png) [@buhrmi](https://discuss.hotwired.dev/u/buhrmi)\
**Post date:** [December 23, 2020, 7:10pm UTC](https://discuss.hotwired.dev/t/using-coffeescript-2-with-stimulus-autoloader/1604/1 "2020-12-23T19:10:01Z")

</div>

Hi there,

I’ve followed the Hotwire introduction video and now I’m wondering if I can somehow bring Stimulus to autoload controllers that are written in CoffeeScript, e.g.

```auto
assets
└── javascripts
    └── controllers
          └── reset_form_controller.coffee

```

So far I’ve added the `coffee-rails` gem, but that does not seem to do the trick. Any hints/ideas?

---

<div class="post-metadata">

**Author:** ![ycrepeau](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/ycrepeau/32/2021_2.png) [@ycrepeau](https://discuss.hotwired.dev/u/ycrepeau)\
**Post date:** [May 30, 2021, 9:36pm UTC](https://discuss.hotwired.dev/t/using-coffeescript-2-with-stimulus-autoloader/1604/2 "2021-05-30T21:36:50Z")

</div>

Since Rails 6, the javascript content is not handle pas sprocket (app/asset/javasript) but by webpacker (app/javascript). The Stimulus controllers are installed in /app/javasript/controllers/ by default.

The coffescript gem is useless in these circumstances. You need to install Coffeescript with Webpacker. You also need to edit /app/javascript/controllers/index.js to handle the context in such a way it accepts .coffe file as well.

And even then, I am not sure that will work.

---

<div class="post-metadata">

**Author:** ![ycrepeau](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/ycrepeau/32/2021_2.png) [@ycrepeau](https://discuss.hotwired.dev/u/ycrepeau)\
**Post date:** [May 30, 2021, 9:47pm UTC](https://discuss.hotwired.dev/t/using-coffeescript-2-with-stimulus-autoloader/1604/3 "2021-05-30T21:47:10Z")

</div>

# /app/javascript/controllers/index.js

```javascript
// Load all the controllers within this directory and all subdirectories.
// Controller files must be named *_controller.js.

import { Application } from "stimulus";
import { definitionsFromContext } from "stimulus/webpack-helpers";

const application = Application.start();
const context = require.context(
  "controllers",
  true,
  /_controller\.(coffee|js)$/
);

application.load(definitionsFromContext(context));

```

## /app/javascript/bonjour\_controller.coffee

```coffee
import { Controller } from "stimulus"

export default class extends Controller 

  connect: ->
    console.log 'bonjour controller connected'
    this.element.textContent = 'Allo'

```

# /app/views/home/index.haml

```haml
%h1 Home#index
%p Find me in app/views/home/index.html.haml

%div{data: {controller: 'bonjour'}}
  %input{type:'text'}
  %button Greet 

```

---

<div class="post-metadata">

**Author:** ![ycrepeau](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/ycrepeau/32/2021_2.png) [@ycrepeau](https://discuss.hotwired.dev/u/ycrepeau)\
**Post date:** [May 30, 2021, 9:48pm UTC](https://discuss.hotwired.dev/t/using-coffeescript-2-with-stimulus-autoloader/1604/4 "2021-05-30T21:48:11Z")

</div>

![20210530_174640_Spectacle](https://canada1.discourse-cdn.com/flex027/uploads/stimulusjs/original/2X/c/c836c2d31ed9a20caa974a851fca8609ec15d86e.jpeg)

---

<div class="post-metadata">

**Author:** ![ranska](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/ranska/32/2490_2.png) [@ranska](https://discuss.hotwired.dev/u/ranska)\
**Post date:** [January 15, 2022, 10:36am UTC](https://discuss.hotwired.dev/t/using-coffeescript-2-with-stimulus-autoloader/1604/5 "2022-01-15T10:36:02Z")

</div>

Hello,  
Thanks for this post.  
I would like to know if you have try with rails 7 ?  
I have try what you do with rails 7 but the js look used in a different way.

I did successed to use definitionsFromContext I got error message
