Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker setup throwing : error TS2307: Cannot find module 'node:http' or its corresponding type declarations. #2184

Closed
tony-garand opened this issue Feb 9, 2024 · 2 comments
Labels

Comments

@tony-garand
Copy link

What version of Hono are you using?

4.0.0

What runtime/platform is your app running on?

Node.js

What steps can reproduce the bug?

Follow the documentation on this page:
https://hono.dev/getting-started/nodejs

npm create hono@latest my-app
cd my-app
npm i

You will have this in your index.ts

import { serve } from '@hono/node-server'
import { Hono } from 'hono'

const app = new Hono()

app.get('/', (c) => {
  return c.text('Hello Hono!')
})

const port = 3005
console.log(`Server is running on port ${port}`)

serve({
  fetch: app.fetch,
  port
})

Creating the Dockerfile

FROM node:20-alpine AS base

FROM base AS builder

RUN apk add --no-cache libc6-compat
WORKDIR /app

COPY package*json tsconfig.json src ./

RUN npm install -g npm@10.4.0

RUN npm i && \
    npm run build && \
    npm prune --production

FROM base AS runner
WORKDIR /app

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 hono

COPY --from=builder --chown=hono:nodejs /app/node_modules /app/node_modules
COPY --from=builder --chown=hono:nodejs /app/dist /app/dist

USER hono
EXPOSE 3000

CMD ["node", "/app/dist/index.js"]

The following steps shall be taken in advance.

Add "outDir": ". /dist" to the compilerOptions section tsconfig.json.
Add "exclude": ["node_modules"] to tsconfig.json.
Add "build": "tsc" to script section of package.json.
Run npm install typescript --save-dev.

When you finally go to create the docker image with

docker build -t hono-docker .

You will get the error:

 > [builder 4/4] RUN npm ci &&     npm run build &&     npm prune --production:
1.615 
1.615 added 11 packages, and audited 12 packages in 1s
1.615 
1.615 2 packages are looking for funding
1.615   run `npm fund` for details
1.615 
1.615 found 0 vulnerabilities
1.616 npm notice 
1.616 npm notice New minor version of npm available! 10.2.4 -> 10.4.0
1.616 npm notice Changelog: <https://github.com/npm/cli/releases/tag/v10.4.0>
1.616 npm notice Run `npm install -g npm@10.4.0` to update!
1.616 npm notice 
1.742 
1.742 > build
1.742 > tsc
1.742 
2.555 node_modules/@hono/node-server/dist/listener.d.ts(1,49): error TS2307: Cannot find module 'node:http' or its corresponding type declarations.
2.555 node_modules/@hono/node-server/dist/listener.d.ts(2,57): error TS2307: Cannot find module 'node:http2' or its corresponding type declarations.
2.555 node_modules/@hono/node-server/dist/server.d.ts(1,29): error TS2307: Cannot find module 'node:net' or its corresponding type declarations.
2.555 node_modules/@hono/node-server/dist/types.d.ts(1,105): error TS2307: Cannot find module 'node:http' or its corresponding type declarations.
2.555 node_modules/@hono/node-server/dist/types.d.ts(2,196): error TS2307: Cannot find module 'node:http2' or its corresponding type declarations.
2.555 node_modules/@hono/node-server/dist/types.d.ts(3,82): error TS2307: Cannot find module 'node:https' or its corresponding type declarations.
2.555 node_modules/hono/dist/types/context.d.ts(54,5): error TS18028: Private identifiers are only available when targeting ECMAScript 2015 and higher.
2.555 node_modules/hono/dist/types/context.d.ts(134,5): error TS2300: Duplicate identifier 'render'.
2.556 node_modules/hono/dist/types/hono-base.d.ts(58,5): error TS18028: Private identifiers are only available when targeting ECMAScript 2015 and higher.
2.556 node_modules/hono/dist/types/request.d.ts(16,5): error TS18028: Private identifiers are only available when targeting ECMAScript 2015 and higher.
------
Dockerfile:10
--------------------
   9 |     
  10 | >>> RUN npm ci && \
  11 | >>>     npm run build && \
  12 | >>>     npm prune --production
  13 |     
--------------------
ERROR: failed to solve: process "/bin/sh -c npm ci &&     npm run build &&     npm prune --production" did not complete successfully: exit code: 2

View build details: docker-desktop://dashboard/build/desktop-linux/desktop-linux/4azjzio33qpvfftx7cblxkp8j

What is the expected behavior?

No response

What do you see instead?

No response

Additional information

No response

@tony-garand tony-garand added the bug label Feb 9, 2024
@usualoma
Copy link
Member

Hi @tony-garand, Thank you for reporting.

https://hono.dev/getting-started/nodejs may need to be more informative. In my environment, I added the following settings, and the build proceeded.

diff --git a/package.json b/package.json
index bfaa41b..edf2fc0 100644
--- a/package.json
+++ b/package.json
@@ -8,6 +8,7 @@
     "hono": "^4.0.0"
   },
   "devDependencies": {
+    "@types/node": "^20.11.17",
     "tsx": "^3.12.2",
     "typescript": "^5.3.3"
   }
diff --git a/tsconfig.json b/tsconfig.json
index 1a148e1..bc32cb8 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -1,5 +1,8 @@
 {
   "compilerOptions": {
+    "target": "ES2020",
+    "module": "Node16",
+    "moduleResolution": "Node16",
     "strict": true,
     "jsx": "react-jsx",
     "jsxImportSource": "hono/jsx",

However, until the following PR is merged, v4.0.0 will not be buildable with tsc.

#2191

@yusukebe
Copy link
Member

Hi!

Yes, we have to set up tsconfig.json correctly. To let users use the correct tsconfig.json, I've updated the starter for Node.js:

honojs/starter@404928f

I'll close this issue. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants