[JS] Different import behavior between assets and node_modules

Hi, I found that the import behavior is different between assets and node_modules if there is extension specified at the end of path.

{{ $js := resources.Get "js/index.ts" }}
{{ $js = $js | js.Build }}
<script src="{{ $js.Permalink }}"></script>
// assets/js/index.ts
import 'foo/index.js'; // Specify the extension as `js`.

// assets/foo/index.ts

// node_modules/foo/index.ts

The node_modules/foo/index.ts will be imported, which is unexpected.


β”œβ”€β”€ assets
β”‚   β”œβ”€β”€ foo
β”‚   β”‚   └── index.ts
β”‚   └── js
β”‚       └── index.ts
β”œβ”€β”€ node_modules
β”‚   └── foo
β”‚       └── index.ts

Full example can be found on GitHub - razonyang/hugo-lab at typescript-import.


I want to create a WorkBox module that mount JS lib on assets, but can’t resolve the dependencies, since their source code import modules with .js ext.
For example: workbox/index.ts at v6 Β· GoogleChrome/workbox Β· GitHub.

The logic in Hugo is 1) Try to find it in /assets 2) If not, let ESBuild find it in node_modules

I find the above behaviour a little surprising, but if Node/ESBuild supports this, then Hugo should. But you need to create a GitHub issue.

As to a workaround, I think you can import ts files into js files, so if you instead create


wouldn’t that work?

1 Like

Yes, rename to assets/foo/index.js works, but the third party source code (mount on assets) is out of my control. The source files are saved with ts ext, but import with js ext.

// assets/js/index.ts 
import 'third-party'; // import third party lib.

/* Third party codes out of my control that mounted by module. */

// assets/third-party/index.ts
import third-party/foo.js // imports with .js ext, but there isn't foo.js.

// assets/third-party/foo.ts 

Thanks for the quick fix, what’s the next release name will be?
I want to specify the required Hugo version on my WorkBox module in advance.

1 Like


1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.