I have a theme that is imported as a module to the site, it has some npm dependencies that I have now managed to get installed with the new hugo mod npm packcommand, however, it is not able to build the JS files.
index.js from the module:
import lazySizes from ‘lazysizes’;
import 'lazysizes/plugins/blur-up/ls.blur-up';
import { noscript } from "lazysizes/plugins/noscript/ls.noscript.js";
import quicklink from "quicklink";
quicklink();
partials/scripts.html, which is called in _baseof.html:
{{ $js := resources.Get "index.js" | js.Build (dict "target" "es2016") }}
{{ if not site.IsServer }}
{{ $js = $js | minify | fingerprint }}
{{ end }}
{{ with $js }}
<script type="text/javascript" src="{{ .RelPermalink }}" defer></script>
{{ end }}
When I try to build or start the server I get this error:
Error: Error building site: JSBUILD: failed to transform “index.js” (application/javascript): No matching export for import “default”
When I use modules I use webpack to create my javascript files and THEN let Hugo work with that. It’s as simple as adding a script section to your package.json and there:
"build": "cross-env NODE_ENV=production webpack --progress --hide-modules && hugo --minify"
It would work like this if the npm dependencies was not loaded from a Hugo module. Since Hugo now can do PostCSS and JS processing (ESBuild) I would like to avoid having to install more tooling to compile the JS.
@ olafghanizadeh Were you able to find the solution for this issue? I’m facing the exact same issue while using JS Processing. Any help would be appreciated.
Error: Error building site: JSBUILD: failed to transform "main.ts" (application/typescript): Could not resolve "@something/XYZ" (mark it as external to exclude it from the bundle).
Note: Tried hugo mod npm pack it did create package.hugo.json and moved some dependencies automatically from deveDependencies to dependencies. Also performed npm install.
I think you need to transpile your TypeScript file before Hugo can deal with it? I haven’t really used TypeScript with a Hugo project before so I can’t say for sure, but check the documentation on the Babel pipe.
As I’m using an internal package I couldn’t setup a test repo with that. The problem seems to be coming from the internal package which is dependent on another package. Have made changes to load those modules like below fixed the issue. Thanks.
main.ts
import "@something/XYZ";
import * as abc from "@something/abc";