title: How to add a library to webpacker to use in Stimulus JS Controllers
body: <p>I want to to use the <a href="https://github.com/iamkun/dayjs" rel="nofollow noreferrer">dayjs</a> library (<a href="https://github.com/you-dont-need/You-Dont-Need-Momentjs" rel="nofollow noreferrer">instead of moment)</a> within my <a href="https://stimulusjs.org/" rel="nofollow noreferrer">Stimulus</a> js Controller, but I keep getting an error that says: </p>
  
  <blockquote>
    <p>dayjs is not defined</p>
  </blockquote>
  
  <pre><code>setNextVisit(event) {
    console.log( dayjs().format('YYYY-MM-DD') );
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/8btRc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8btRc.png" alt="enter image description here"></a></p>
  
  <p>I'm using Rails 5.2 with Webpacker and Stimulus.</p>
  
  <p>I added <em>dayjs</em> via <code>yarn add dayjs --save</code> which add this to <code>/package.json</code>:</p>
  
  <p><strong>/package.json</strong></p>
  
  <pre><code>{
    "name": "meh",
    "private": true,
    "dependencies": {
      "@rails/ujs": "^6.0.0-alpha",
      "@rails/webpacker": "3.5",
      "dayjs": "^1.8.2"
      "stimulus": "^1.1.1",
      "turbolinks": "^5.2.0"
    },
    "devDependencies": {
      "webpack-dev-server": "2.11.2"
    }
  }
  </code></pre>
  
  <p>To wire-up dayjs in webpack via the webpacker gem, I've added this to <code>/app/javascript/packs/application.js</code>:</p>
  
  <p><strong>/app/javascript/packs/application.js</strong></p>
  
  <pre><code>/* eslint no-console:0 */
  
  // @RAILS/UJS
  // imported in config/webpack/environment.js
  Rails.start();
  
  // TURBOLINKS
  import Turbolinks from 'turbolinks';
  Turbolinks.start();
  
  // DAYJS
  import dayjs from 'dayjs'
  
  // STIMULUS
  import { Application } from 'stimulus'
  import { definitionsFromContext } from "stimulus/webpack-helpers"
  
  
  const application = Application.start()
  const context = require.context("./controllers", true, /\.js$/)
  application.load(definitionsFromContext(context))
  </code></pre>