title: Ruby on Rails 6 - implementing javascripts (chess.js)
body: <p><strong>Looking for help with application of custom javascript in Rails 6 framework.</strong> I am trying to implement the small example coming from chess.js module (<a href="https://github.com/jhlywa/chess.js/blob/master/README.md" rel="nofollow noreferrer">https://github.com/jhlywa/chess.js/blob/master/README.md</a>) in my rails 6 newly set up app  (I am running the in ubuntu subsystem on windows) which should trigger a random game of chess in the console.</p>
  <pre><code>index.html.erb  
  
  &lt;script&gt; 
  
    const chess = new Chess()
  
    while (!chess.game_over()) {
        const moves = chess.moves()
        const move = moves[Math.floor(Math.random()*moves.length)]
        chess.move(move)
    }
    console.log(chess.pgn())      
  
    &lt;/script&gt;
  </code></pre>
  <p>My understanding is that Rails 6 uses webpacker for managing scripts like that and once I install the modules through npm and yarn I should be able to use the functionality without adding the chess.js file to the /app/javascript/ folder manually.</p>
  <pre><code># NPM
  npm install chess.js
  
  # Yarn
  yarn add chess.js 
  </code></pre>
  <p>(I can see that chess.js have been added under config/node_modules/)</p>
  <p><strong>When I run above snippet I get an error:</strong></p>
  <pre><code>Uncaught ReferenceError: require is not defined
  </code></pre>
  <p><strong>and when I omit the first line in the above snippet then I get:</strong></p>
  <pre><code>Uncaught ReferenceError: Chess is not defined
  </code></pre>
  <br>
  <p>I was trying also to place the chess.js in the  /app/javascript/packs folder and then load that through</p>
  <pre><code>import &quot;chess.js&quot;
  require(&quot;channels&quot;)
  require(&quot;packs/chess&quot;)
  </code></pre>
  <p>in the application.js, but it is all leaping in the dark with my little experience with the Rails framework.
  <br>
  <strong>Would you please demonstrate correct implementation of this small example? Is it possible to implement this script in the Rails 6 framework?</strong></p>