docker 部署失败:找不到模块“/app/heroku”

问题描述 投票:0回答:1

我在 Docker 中有一个 Node 应用程序,我正在尝试使用此部署 github 操作部署到 Heroku:https://github.com/AkhileshNS/heroku-deploy/tree/master

我的应用程序已在 Heroku 中创建,并且使用默认的节点构建包。从各方面来看,GHA 方面的部署似乎都取得了成功。

这是我的 Dockerfile:

FROM node:20 AS base 
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
CMD ["node", "./src/index.ts"]

这是我的package.json:

{
  "name": "goal-service",
  "version": "1.0.0",
  "description": "Service that powers the Goal API",
  "main": "index.js",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/onarollonaroll/goal-service.git"
  },
  "engines": {
    "node": "20.x",
    "npm": "10.x"
  },
  "author": "Onaroll",
  "license": "ISC",
  "scripts": {
    "compile": "tsc",
    "dev": "ts-node-dev --respawn ./src/index.ts",
    "test": "jest",
    "format": "npx prettier --write .",
    "start": "node ./src/index.ts"
  },
  "dependencies": {
    "@graphql-tools/schema": "^10.0.3",
    "@parcel/watcher": "^2.4.0",
    "graphql": "^16.8.1",
    "graphql-tag": "^2.12.6",
    "graphql-yoga": "^5.2.0",
    "heroku": "^8.11.0"
  },
  "devDependencies": {
    "@types/jest": "^29.0.3",
    "@types/node": "^20.0.0",
    "husky": "^9.0.11",
    "jest": "^29.0.3",
    "nodemon": "^3.0.0",
    "prettier": "^3.2.5",
    "pretty-quick": "^4.0.0",
    "ts-jest": "^29.0.2",
    "ts-node": "^10.9.1",
    "ts-node-dev": "^2.0.0",
    "typescript": "^5.4.3"
  },
  "jest": {
    "preset": "ts-jest",
    "testEnvironment": "node",
    "roots": [
      "test"
    ]
  },
  "husky": {
    "hooks": {
      "pre-commit": "pretty-quick --staged"
    }
  }
}

这是我的文件夹结构(仅显示相关文件):

├── README.md
├── src
│   ├── index.ts
├── package.json
├── package-lock.json
├── Dockerfile
├── docker-compose.yml

这是我的

index.ts
(如果需要):

import { readFileSync } from 'fs'
import path from 'path'
import { gql } from 'graphql-tag'
import { createYoga } from 'graphql-yoga'
import { createServer } from 'http'
import { makeExecutableSchema } from '@graphql-tools/schema'
import { resolvers } from './resolvers'

const typeDefs = gql(
  readFileSync(path.resolve(__dirname, './schema.graphql'), {
    encoding: 'utf-8',
  })
)

function startYogaServer() {
  const schema = makeExecutableSchema({ typeDefs, resolvers })
  const yoga = createYoga({ schema })
  const server = createServer(yoga)
  server.listen(4000, () => {
    console.info('Server is running on http://localhost:4000/graphql')
  })
}

startYogaServer()

如果我尝试通过 Heroku 打开应用程序,我会收到一个标准的“应用程序错误”,告诉我检查日志。

在日志中我看到此错误:

node:internal/modules/cjs/loader:1147
  throw err;
  ^

Error: Cannot find module '/app/heroku'
    at Module._resolveFilename (node:internal/modules/cjs/loader:1144:15)
    at Module._load (node:internal/modules/cjs/loader:985:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
    at node:internal/main/run_main_module:28:49 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

Node.js v20.11.1

我认为我的应用程序不需要安装 Heroku,但我还是尝试安装它 (

npm install heroku
),但仍然遇到相同的错误。

我在其他地方看到一个建议,建议我在 Heroku 中将

NODE_MODULES_CACHE
设置为 false,我也这样做了,但没有效果。

关于我在这里做错了什么有什么想法吗?

编辑:我现在认为上述错误可能是通过heroku网站上的heroku CLI运行

heroku logs --tail
引起的错误,因为也许我的容器没有安装Heroku?

当我在服务的 heroku 页面的下拉列表中单击“查看日志”时,我会看到以下日志:

2024-03-26T13:48:40.543188+00:00 app[web.1]: 
2024-03-26T13:48:40.543188+00:00 app[web.1]: Node.js v20.11.1
2024-03-26T13:48:40.606107+00:00 heroku[web.1]: Process exited with status 1
2024-03-26T13:48:40.627333+00:00 heroku[web.1]: State changed from starting to crashed
2024-03-26T13:48:42.941754+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=goal-service-staging-3c32681ee39d.herokuapp.com request_id=d654a8fe-0ceb-46c5-b2ea-2ae63a7fe2d5 fwd="72.74.249.49" dyno= connect= service= status=503 bytes= protocol=https
2024-03-26T13:48:43.066833+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=goal-service-staging-3c32681ee39d.herokuapp.com request_id=68a2d6fa-e9b3-422e-a80b-bab6cbad21e8 fwd="72.74.249.49" dyno= connect= service= status=503 bytes= protocol=https
2024-03-26T13:49:01.893680+00:00 app[api]: Starting process with command `heroku logs --tail` by user [email protected]
2024-03-26T13:49:18.874678+00:00 heroku[run.5163]: State changed from starting to up
2024-03-26T13:49:22.565432+00:00 heroku[run.5163]: Process exited with status 1
2024-03-26T13:49:22.590829+00:00 heroku[run.5163]: State changed from up to complete

我不确定是否提供任何附加信息。我看不出导致 503 的原因。

node.js docker npm heroku
1个回答
0
投票

我的答案最终是调整我的

Dockerfile
以正确启动应用程序。

因此,这是我的新

Dockerfile

FROM node:20 AS base 
WORKDIR /app
COPY package.json .
RUN npm install && npm install ts-node -g
COPY . .
CMD ["ts-node", "src/index.ts"]

我需要使用

ts-node
来运行该应用程序,并且需要全局安装它才能在这里工作。

© www.soinside.com 2019 - 2024. All rights reserved.