common/hexec: Make NODE_PATH a fallback for ESM bare imports

Node's ESM resolver does not consult NODE_PATH (unlike CJS require), so
an ESM postcss.config.js shipped by a Hugo theme fails when loaded from
the module cache: bare imports like `import x from "postcss-import"`
have no node_modules to walk up to.

Install a synchronous resolver hook (module.registerHooks) via
--import=data:... on every Node invocation. On ERR_MODULE_NOT_FOUND for
a bare specifier it resolves the package from each NODE_PATH entry via
createRequire().resolve(). No-op for relative, absolute, URL-scheme and
non-MODULE_NOT_FOUND failures. Synchronous hooks run on the main thread,
so no --allow-worker is needed under the Node permission model.

Fixes #13987

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Bjørn Erik Pedersen
2026-05-13 17:05:09 +02:00
parent 123018de21
commit ae7bf74b3e
4 changed files with 157 additions and 0 deletions
@@ -15,6 +15,7 @@ package cssjs_test
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
@@ -214,3 +215,73 @@ Styles Content: Len: 770917
}
}
// See Issue 13987.
func TestTransformPostCSSESMConfigInModule(t *testing.T) {
if !htesting.IsCI() {
t.Skip("Skip long running test when running locally")
}
c := qt.New(t)
// Use htesting.CreateTempDir to get canonical paths on macOS
// (/private/var/...); Node's --permission model rejects the symlinked
// /var/folders/... form when crossing the project boundary.
rootDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-integration-test")
c.Assert(err, qt.IsNil)
c.Cleanup(clean)
projectDir := filepath.Join(rootDir, "project")
moduleDir := filepath.Join(rootDir, "external-module")
c.Assert(os.MkdirAll(projectDir, 0o755), qt.IsNil)
c.Assert(os.MkdirAll(moduleDir, 0o755), qt.IsNil)
files := `
-- hugo.toml --
disableKinds = ['taxonomy', 'term', 'page']
baseURL = "https://example.com"
[[module.imports]]
path = "github.com/bep/hugo-mod-nop"
-- assets/css/styles.css --
body { color: red }
-- layouts/home.html --
{{ $styles := resources.Get "css/styles.css" | css.PostCSS }}
RelPermalink: {{ $styles.RelPermalink }}|HasBody: {{ in $styles.Content "color:" }}|
-- content/_index.md --
---
title: home
---
-- package.json --
{
"devDependencies": {
"postcss-cli": "11.0.0",
"postcss-import": "16.0.0"
}
}
-- go.mod --
module github.com/example/project
go 1.20
replace github.com/bep/hugo-mod-nop => ../external-module
-- ../external-module/go.mod --
module github.com/bep/hugo-mod-nop
go 1.20
-- ../external-module/postcss.config.js --
import postcssImport from "postcss-import";
export default { plugins: [postcssImport()] };
`
b := hugolib.Test(c, files,
hugolib.TestOptWithConfig(func(cfg *hugolib.IntegrationTestConfig) {
cfg.WorkingDir = projectDir
cfg.NeedsOsFS = true
cfg.NeedsNpmInstall = true
}),
)
b.AssertFileContent("public/index.html",
"RelPermalink: /css/styles.css|HasBody: true|",
)
}