NPM/YARN with drupal-libraries-sync
The drupal-libraries-sync tool bridges the gap between NPM/Yarn package management and Drupal's library system. It copies the necessary files from node_modules/ into Drupal's libraries/ directory, making them available to modules and themes that expect libraries in that location.
How It Works
Library dependencies are declared in the project's
package.jsonfile.Running
npm installoryarn installdownloads the packages tonode_modules/.The
drupal-libraries-synccommand copies the relevant files fromnode_modules/to thelibraries/directory based on a mapping configuration.
Setting Up package.json
Your project's package.json file should include the front-end libraries as dependencies and a script entry for running the sync. In Varbase 11.0.x the sync script is shipped with the varbase_starter recipe at recipes/varbase_starter/scripts/drupal-libraries-sync.js:
{
"name": "my-varbase-project",
"version": "1.0.0",
"description": "Front-end libraries for my Varbase project",
"scripts": {
"drupal-libraries-sync": "node ./recipes/varbase_starter/scripts/drupal-libraries-sync.js",
"postinstall": "node ./recipes/varbase_starter/scripts/drupal-libraries-sync.js"
},
"dependencies": {
"ace-builds": "~1",
"aos": "~2",
"dropzone": "~5",
"jquery.fancytree": "~2",
"swagger-ui-dist": "~3"
}
}The postinstall hook makes the sync run automatically after yarn install or npm install, so libraries are always in sync with the installed packages.
Installing Libraries
To install all libraries defined in package.json:
Or with Yarn:
This downloads all packages to the node_modules/ directory.
Running the Sync
After installing packages, run the sync command to copy library files to Drupal's libraries/ directory:
Or invoke the script directly:
The sync runs automatically as a postinstall hook, so yarn install (or npm install) already produces an up-to-date libraries/ directory.
Configuration
The sync script reads a drupal-libraries section in package.json defining the target libraries directory and the list of packages to sync:
Each entry defines:
library-directory: The destination directory for synced libraries (relative to the project root).
name: The directory name created under the library directory (e.g.
web/libraries/ace). Slashes in the name are honored as nested folders.package: The NPM package path under
node_modules/to copy from. Append a subpath (e.g.dropzone/dist) to copy only a specific folder of the package.
Adding a New Library
To add a new front-end library to your Varbase project:
Install the NPM package:
Add an entry to the
drupal-libraries.librariesarray inpackage.jsonwith anameandpackage.Run the sync command:
Verify that the library files appear in the
libraries/directory.
Updating Libraries
To update a library to a newer version:
Update the package version in
package.jsonor run:
Re-run the sync command:
Test the functionality that depends on the updated library.
Integration with Composer
For Varbase projects, the library sync is integrated into the Composer workflow via dedicated drupal-libraries-sync, drupal-libraries-yarn-sync, and drupal-libraries-npm-sync Composer script entries. These run yarn install (or npm install) followed by the sync script, so a fresh composer install ends with an up-to-date web/libraries/ directory.
In composer.json:
Best Practices
Track package.json in version control: Always commit your
package.jsonandpackage-lock.json(oryarn.lock) to ensure reproducible builds.Ignore node_modules and libraries in Git: Add
node_modules/andlibraries/to.gitignoresince they can be regenerated frompackage.json.Pin library versions: Use specific version ranges in
package.jsonto prevent unexpected updates.Automate the sync: Integrate the sync into your CI/CD pipeline and Composer scripts to ensure libraries are always up to date.
Last updated