Including javascript lib from node modules

I’m new to Hugo but I’m wondering how to include jaavascript libs from node_modules?

I was able to it with css like:

{{ $options := (dict "targetPath" "style.css" "outputStyle" "compressed") }}
{{ $style := resources.Get "sass/main.scss" | toCSS $options }}

and my assets/sass/main.scss contains:

@import "node_modules/bootstrap/scss/bootstrap";
@import "node_modules/@fortawesome/fontawesome-pro/scss/fontawesome";

this works nicely.

But I cannot find any documentation how to replace below script inclusions so they are retrieved from nodule_modules as well.

<script src="../../assets/js/vendor/popper.min.js"></script>
<script src="../../dist/js/bootstrap.min.js"></script>

To use load resources via resources.Get etc. from node_modules you need to mount them into your project (typically /assets, but you may need to mount the fontawesome fonts below /static).

Also note that if you’re not a big fan of NPM, you can mount GitHub projects directly, or use prepared Hugo Modules, e.g.

3 Likes

Thx a lot!
The mounts work (and are also needed for the fontawesome webfonts).

1 Like