Error while deploying Node.js app

I’d be glad to take a look at it. My github id is rubys.

Got your repository. Reproduced the error. With the following change, I can now build:

RUN npm prune --production --legacy-peer-deps

Notes: because your only devDependency is nodemon, the prune is optional as it won’t save much space. Furthermore, since you don’t have a build step, you can omit --production=false on the install step. I’m still working on optimizing the Dockerfile.

2 Likes

Issue opened to make it an option on the docker file generation: support a --legacy-peer-deps option · Issue #6 · fly-apps/dockerfile-node · GitHub

1 Like

I’ve been blocked from making any comments since yesterday because I exceeded the new user comments limit. I saw you started an issue on GitHub. I implemented your last solution and my terminal displays that the build and deployment are successful, but the project on fly.io dashboard is suspended. The monitoring tab on the dashboard displays the following error:

2023-04-29T16:30:23.024 app[3287335a3020e8] cdg [info] Starting init (commit: 15c0f38)...

2023-04-29T16:30:23.045 app[3287335a3020e8] cdg [info] Preparing to run: `docker-entrypoint.sh npm run start` as root

2023-04-29T16:30:23.075 app[3287335a3020e8] cdg [info] 2023/04/29 16:30:23 listening on [fdaa:2:12c7:a7b:aec1:9358:ef5b:2]:22 (DNS: [fdaa::3]:53)

2023-04-29T16:30:24.076 app[3287335a3020e8] cdg [info] npm ERR! Missing script: "start"

2023-04-29T16:30:24.076 app[3287335a3020e8] cdg [info] npm ERR!

2023-04-29T16:30:24.077 app[3287335a3020e8] cdg [info] npm ERR! Did you mean one of these?

2023-04-29T16:30:24.077 app[3287335a3020e8] cdg [info] npm ERR! npm star # Mark your favorite packages

2023-04-29T16:30:24.077 app[3287335a3020e8] cdg [info] npm ERR! npm stars # View packages marked as favorites

2023-04-29T16:30:24.080 app[3287335a3020e8] cdg [info] npm ERR!

2023-04-29T16:30:24.080 app[3287335a3020e8] cdg [info] npm ERR! To see a list of scripts, run:

2023-04-29T16:30:24.080 app[3287335a3020e8] cdg [info] npm ERR! npm run

2023-04-29T16:30:24.080 app[3287335a3020e8] cdg [info] npm ERR! A complete log of this run can be found in:

2023-04-29T16:30:24.081 app[3287335a3020e8] cdg [info] npm ERR! /root/.npm/_logs/2023-04-29T16_30_23_978Z-debug-0.log

2023-04-29T16:30:25.066 app[3287335a3020e8] cdg [info] Starting clean up.

2023-04-29T16:30:26.069 app[3287335a3020e8] cdg [info] [ 3.135865] reboot: Restarting system

2023-04-29T16:30:27.006 runner[3287335a3020e8] cdg [info] machine has reached its max restart count (10)

which indicates that the error is caused because of a missing “start” script.

Finally my app is up and running thanks to you. I really appreciate what you have done so far, thank you so much for help.
The final solution is updating the Dockerfile to the following:

# syntax = docker/dockerfile:1

ARG NODE_VERSION=18.15.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 

COPY <<-"EOF" /app/package.json
{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "nodemon index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.20.2",
    "cors": "^2.8.5",
    "dotenv": "^16.0.3",
    "express": "^4.18.2",
    "helmet": "^6.0.1",
    "mongoose": "^7.0.0",
    "mongoose-currency": "^0.2.0",
    "morgan": "^1.10.0"
  },
  "devDependencies": {
    "nodemon": "^2.0.21"
  }
}
EOF

RUN npm install --legacy-peer-deps

# Copy application code
COPY --link . .

# Remove development dependencies
RUN npm prune --production --legacy-peer-deps

# 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 updating the package.json file to include a “start” script to be the following:

{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "nodemon index.js",
    "start": "node index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.20.2",
    "cors": "^2.8.5",
    "dotenv": "^16.0.3",
    "express": "^4.18.2",
    "helmet": "^6.0.1",
    "mongoose": "^7.0.0",
    "mongoose-currency": "^0.2.0",
    "morgan": "^1.10.0"
  },
  "devDependencies": {
    "nodemon": "^2.0.21"
  }
}
1 Like

Now that it is working, you can replace this (through the EOF) with:

COPY package.json package-lock.json .

On it. I’ll update you if anything went wrong. Thank you for your help.

@kareemabbas how did you get your mongodb atlas to connect? As we can’t seem to get it connecting without allowing all public IP addresses to access it.

This is how I connected my mongoDB Atlas. This is not a commercial application, thus it’s okay for me to allow all public IP address to access it for the current. Sorry for the late reply though.

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