HUGO

Importing from node_modules with js.Build uses "require" / CommonJS

I’m trying to include node_modules with Hugo Pipes, in exactly the way which was described here. But the output is something like that:

var import_underscore = __toModule(require("underscore"));

… which, of course, does not work in the browser. Am I supposed to enable “require” with Browserify etc.?

Thanks a lot!

You seem to mix two things together. To use javascript modules, you need to use JSBuild which compiles the “required” modules into one single JS file. I am not sure, but “CommonJS” sounds like a very old concept to me (RequireJS?).

Did you read through the JSBuild docs on hugo.io?

It’s probably best if you post some samples of how your JS is compiled (in a layout) and the JS itself to point you to issues.

The above suggests to me that you use the Externals option to “load” underscore (this doesn’t work, and that option isn’t relevant in the browser).

You may get some input here:

Okay, I found my mistake. I’ve tried to address node modules within my theme folder (my JS file is also in themes/theme-name/assets/js/). This does not work. As soon as I put my package.json in the root folder and install the node modules, it works just fine! (And yes, also with _underscore.js). Don’t know what’s the best way to include node dependencies in a theme, but probably that’s another topic. Thanks!