How do you include 3rd party libraries like js-cookie in javascripts that are under app/assets/javascripts?

When reading the README for webpacker, it seems to state that javascripts that fall under app/javascript/packs is for “app-like” javascript code.

“Webpacker makes it easy to use the JavaScript pre-processor and bundler Webpack v5 to manage application-like JavaScript in Rails. It coexists with the asset pipeline, as the primary purpose for webpack is app-like JavaScript, not images, CSS, or even JavaScript Sprinkles (that all continues to live in app/assets).”

Ok, so I have some JavaScripts in app/assets/javascripts, I’m guessing this is managed by the “asset pipeline?” AKA sprockets?

When I try to include the library ‘js-cookie’ in the manifest file app/assets/javascripts/application.js via: //= require js-cookie I end up getting an error in the browser console that says “module is not defined”

Ok, I found a solution. I’m not sure if this is the correct or preferred way, but it works.

//= require js-cookie/dist/js.cookie.min.js

That’s the correct Sprockets way. app/assets/javascripts/application.js is a Sprockets manifest file and it can reference local files (in app/assets/) or in node_modules

In general, you find a JS lib you want to use, then npm install or yarn install it.

Then look in /node_modules for the files (and path to file) to reference in the sprockets manifest files (both css & js if necessary).

Just like you did