Closed iceniveth closed 1 week ago
Hi @iceniveth
How about adding .js
to the import statement?
import aboutRoute from "./aboutRoute.js";
I think it does not matter just for Node.js Docker.
Okay, will close this PR.
I kind-a used to this auto-import shortcut from VSCode which it doesn't put an extension:
Not sure if there is a way to automatically put .js
at the end (although my file is aboutRoute.ts
). Or might just manually put it. 👻
Description
The goal of this PR is to fix
Error [ERR_MODULE_NOT_FOUND]: Cannot find module
when building the app usingdocker compose build
. This seems to occur when using ESM imports w/o file extension.To reproduce the issue (reproduction repo)
I'll create a file
src/aboutRoute.ts
with the content:import that file in the Hono app
src/index.ts
Create a
docker-compose.yml
file:Checking the
dist/index.js
viadocker compose run --rm app cat dist/index.js
shows thatwhich I think NodeJS Doesn't support extensionless import. Saw it on TS docs.
Propose Fix
To use tsc-alias, to resolve full paths. That way, the build output will replace incomplete import paths (those not ending in .js) with fully resolved paths (for ECMAScript Modules compatibility)
Here's a working example repo.
Access
http://localhost:3000/about
w/o errors.Update the docs for https://hono.dev/docs/getting-started/nodejs#dockerfile
Alternative Fix
If not tsc-alias, I see another approach is to use extensionless. I got it working as well repo
Access
http://localhost:3000/about
w/o errors.