
We typically use NPM on projects, but if you’re using Yarn or another tool the process will be similar. But at the highest level, you’ll be adding your dependency using your preferred package manager. If your JavaScript is a dependency of your theme, the specifics will likely vary a bit based on your front-end tooling and workflow. JavaScript That is a Dependency of Your Theme Given how common Composer is on Drupal projects and NPM is on front end projects, this isn’t a very controversial assumption, but it does dictate a number of things related to the approach.

Secondly, our preference is to use package managers like Composer and NPM to easily import external dependencies. This allows greater control over items like aggregation and would also allow all of the assets in your project to be hosted on the same Content Delivery Network (CDN). Assumptionsīefore we dive into specifics, let’s first talk about a few assumptions that we had as a team.įirst off, all of these approaches assume that our preference is to have JavaScript as a local dependency rather than externally hosted. Let's explore the different ways you can include a third-party JavaScript library into your next build - either as a dependency of your theme, custom module, or overall project. The approach to add a JavaScript library can change if you view the task as front end versus back end.

How could that possibly be? As is often the case in the world of Drupal, there are a number of valid approaches with subtle but relevant differences. To the surprise of many, we found that this common task still inspired quite a bit of discussion.

In a recent internal discussion, the topic of incorporating an external JavaScript dependency into a Drupal project came up.
