Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HUGO: Include node module in static js

Installed a node package via npm i resize-start-end -S'.

This is the instruction provided by the package:

// ESM
import resizeStartEnd from 'resize-start-end';

// or CommonJS
const resizeStartEnd = require('resize-start-end');

I'm trying to include the package in a JS file in my hugo static directory. None of the above works (can't be found).

My js is located in static/src/js and gulp will concat it into static/js.

Advice appreciated.

like image 907
Aen Tan Avatar asked Dec 11 '17 09:12

Aen Tan


1 Answers

From Hugo version 0.56 you can use Hugo mounts.

After module install (npm i your-module -S)

Add to your config file (json format for example):

"module": {
    "mounts": [
       {
        "source": "node_modules",
        "target": "static/src/node_modules"
       }
    ]
}

Then you can use in your scripts in src directory:

import * as yourModule from './node_modules/your-module/index.js'
like image 171
vitaliytv Avatar answered Oct 23 '22 17:10

vitaliytv