Problems to run a node.js app

Well I have a Node.js bot I’m running it using pm2. In my machine runs ok (obviously). But when I try to run on fly.io never run.

I have the following error:

2024-09-10T17:59:44.046 runner[784e672f2e0dd8] gru [info] Machine started in 1.059s

2024-09-10T17:59:44.925 app[784e672f2e0dd8] gru [info] > start

2024-09-10T17:59:44.925 app[784e672f2e0dd8] gru [info] > pm2 start ./build/main.js

2024-09-10T17:59:45.338 app[784e672f2e0dd8] gru [info] -------------

2024-09-10T17:59:45.338 app[784e672f2e0dd8] gru [info] __/\\\\\\\\\\\\\____/\\\\____________/\\\\____/\\\\\\\\\_____

2024-09-10T17:59:45.338 app[784e672f2e0dd8] gru [info] _\/\\\/////////\\\_\/\\\\\\________/\\\\\\__/\\\///////\\\___

2024-09-10T17:59:45.338 app[784e672f2e0dd8] gru [info] _\/\\\_______\/\\\_\/\\\//\\\____/\\\//\\\_\///______\//\\\__

2024-09-10T17:59:45.338 app[784e672f2e0dd8] gru [info] _\/\\\\\\\\\\\\\/__\/\\\\///\\\/\\\/_\/\\\___________/\\\/___

2024-09-10T17:59:45.338 app[784e672f2e0dd8] gru [info] _\/\\\/////////____\/\\\__\///\\\/___\/\\\________/\\\//_____

2024-09-10T17:59:45.338 app[784e672f2e0dd8] gru [info] _\/\\\_____________\/\\\____\///_____\/\\\_____/\\\//________

2024-09-10T17:59:45.338 app[784e672f2e0dd8] gru [info] _\/\\\_____________\/\\\_____________\/\\\___/\\\/___________

2024-09-10T17:59:45.338 app[784e672f2e0dd8] gru [info] _\/\\\_____________\/\\\_____________\/\\\__/\\\\\\\\\\\\\\\_

2024-09-10T17:59:45.338 app[784e672f2e0dd8] gru [info] _\///______________\///______________\///__\///////////////__

2024-09-10T17:59:45.338 app[784e672f2e0dd8] gru [info] Runtime Edition

2024-09-10T17:59:45.338 app[784e672f2e0dd8] gru [info] PM2 is a Production Process Manager for Node.js applications

2024-09-10T17:59:45.338 app[784e672f2e0dd8] gru [info] with a built-in Load Balancer.

2024-09-10T17:59:45.338 app[784e672f2e0dd8] gru [info] Start and Daemonize any application:

2024-09-10T17:59:45.338 app[784e672f2e0dd8] gru [info] $ pm2 start app.js

2024-09-10T17:59:45.338 app[784e672f2e0dd8] gru [info] Load Balance 4 instances of api.js:

2024-09-10T17:59:45.338 app[784e672f2e0dd8] gru [info] $ pm2 start api.js -i 4

2024-09-10T17:59:45.338 app[784e672f2e0dd8] gru [info] Monitor in production:

2024-09-10T17:59:45.338 app[784e672f2e0dd8] gru [info] $ pm2 monitor

2024-09-10T17:59:45.338 app[784e672f2e0dd8] gru [info] Make pm2 auto-boot at server restart:

2024-09-10T17:59:45.338 app[784e672f2e0dd8] gru [info] $ pm2 startup

2024-09-10T17:59:45.338 app[784e672f2e0dd8] gru [info] To go further checkout:

2024-09-10T17:59:45.338 app[784e672f2e0dd8] gru [info] http://pm2.io/

2024-09-10T17:59:45.338 app[784e672f2e0dd8] gru [info] -------------

2024-09-10T17:59:45.447 app[784e672f2e0dd8] gru [info] [PM2] Spawning PM2 daemon with pm2_home=/root/.pm2

2024-09-10T17:59:45.685 app[784e672f2e0dd8] gru [info] [PM2] PM2 Successfully daemonized

2024-09-10T17:59:45.711 app[784e672f2e0dd8] gru [info] [PM2] Starting /app/build/main.js in fork_mode (1 instance)

2024-09-10T17:59:45.732 app[784e672f2e0dd8] gru [info] [PM2] Done.

2024-09-10T17:59:45.768 app[784e672f2e0dd8] gru [info] ┌────┬─────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐

2024-09-10T17:59:45.768 app[784e672f2e0dd8] gru [info] │ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │

2024-09-10T17:59:45.768 app[784e672f2e0dd8] gru [info] ├────┼─────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤

2024-09-10T17:59:45.768 app[784e672f2e0dd8] gru [info] │ 0 │ main │ default │ N/A │ fork │ 361 │ 0s │ 0 │ online │ 0% │ 36.1mb │ root │ disabled │

2024-09-10T17:59:45.768 app[784e672f2e0dd8] gru [info] └────┴─────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

2024-09-10T17:59:45.785 app[784e672f2e0dd8] gru [info] npm notice

2024-09-10T17:59:45.785 app[784e672f2e0dd8] gru [info] npm notice New patch version of npm available! 10.8.1 -> 10.8.3

2024-09-10T17:59:45.785 app[784e672f2e0dd8] gru [info] npm notice Changelog: https://github.com/npm/cli/releases/tag/v10.8.3

2024-09-10T17:59:45.785 app[784e672f2e0dd8] gru [info] npm notice To update run: npm install -g npm@10.8.3

2024-09-10T17:59:45.785 app[784e672f2e0dd8] gru [info] npm notice

2024-09-10T17:59:45.995 app[784e672f2e0dd8] gru [info] INFO Main child exited normally with code: 0

2024-09-10T17:59:46.009 app[784e672f2e0dd8] gru [info] INFO Starting clean up.

2024-09-10T17:59:46.023 app[784e672f2e0dd8] gru [info] WARN could not unmount /rootfs: EINVAL: Invalid argument

2024-09-10T17:59:46.024 app[784e672f2e0dd8] gru [info] [ 2.844954] reboot: Restarting system

2024-09-10T17:59:46.288 runner[784e672f2e0dd8] gru [info] machine exited with exit code 0, not restarting

Buil script: “build”: “npx tsc”,
Run script: “start”: “pm2 start ./build/main.js”

Dockerfile

# syntax = docker/dockerfile:1

# Adjust NODE_VERSION as desired
ARG NODE_VERSION=20.16.0
FROM node:${NODE_VERSION}-slim as base

LABEL fly_launch_runtime="NodeJS"

# NodeJS app lives here
WORKDIR /app

# Set production environment
ENV NODE_ENV=production


# Throw-away build stage to reduce size of final image
FROM base as build

# Install packages needed to build node modules
RUN apt-get update -qq && \
    apt-get install -y python-is-python3 pkg-config build-essential 

# Install node modules
COPY --link package.json package-lock.json ./
RUN npm install --production=false

# Copy application code
COPY --link . .

# Build application
RUN npm run build

# Remove development dependencies
# RUN npm prune --production


# Final stage for app image
FROM base

# Copy built application
COPY --from=build /app /app

# Start the server by default, this can be overwritten at runtime
CMD [ "npm", "run", "start" ]

and

fly.toml

# fly.toml app configuration file generated for save-to-later on 2024-09-10T14:30:29-03:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = 'save-to-later'
primary_region = 'gru'

[build]

[env]
  PORT = '8080'

[http_service]
  internal_port = 8080
  force_https = true
  auto_stop_machines = 'stop'
  auto_start_machines = true
  min_machines_running = 0
  processes = ['app']

[[vm]]
  memory = '1gb'
  cpu_kind = 'shared'
  cpus = 1

I don’t know PM2, but you probably want --no-daemon. Otherwise all fly.io knows is that the main application (in this case pm2) has existed, so it starts to clean up:

You right, it’s my first time using this lib. I will set this option and try again

Do you really need to use pm2? I don’t think it makes much sense on fly, plus it adds additional overhead.

Sometimes my app crashs and need this to run it. But I will try without it

You’d want your app to crash w/ a non-zero code so the fly proxy can auto restart it.

How can I config this? Sometimes my app carash and machines are suspend

What does your log say? Your config should have restarted your machine when it crashed. Maybe you need to add a health check?

I try only with node and the path. The applicationn runs but after some times show this


2024-09-11T12:29:25.051 app[784e672f2e0dd8] gru [info] INFO Sending signal SIGTERM to main child process w/ PID 323

2024-09-11T12:29:25.529 app[784e672f2e0dd8] gru [info] INFO Main child exited with signal (with signal 'SIGTERM', core dumped? false)

2024-09-11T12:29:25.545 app[784e672f2e0dd8] gru [info] INFO Starting clean up.

2024-09-11T12:29:25.546 app[784e672f2e0dd8] gru [info] WARN could not unmount /rootfs: EINVAL: Invalid argument

2024-09-11T12:29:25.551 app[784e672f2e0dd8] gru [info] [ 462.381516] reboot: Restarting system

Oh, that’s expected, when your traffic is idle, it will autostop:

  auto_stop_machines = 'stop'
  auto_start_machines = true
  min_machines_running = 0

Same config here. I dind’t see it.
auto_stop_machines = ‘stop’
auto_start_machines = true
min_machines_running = 0
So just remove auto_stop_machine solve this problem? Because this bot is running okay in my machine so I delete my fly.io account

Read more about it here App configuration (fly.toml) · Fly Docs