I tried to deploy using fly deploy
but it failed so I followed this tutorial https://youtu.be/uoJ0Tv-BFcQ?list=LL&t=18827
I tried to deploy it using the command fly deploy --local-only
using Docker locally and I got this error:
=> ERROR [build 3/5] RUN npm install --production=false 3.3s
------
> [build 3/5] RUN npm install --production=false:
#10 0.938 npm WARN config production Use `--omit=dev` instead.
#10 3.216 npm ERR! code ERESOLVE
#10 3.226 npm ERR! ERESOLVE could not resolve
#10 3.226 npm ERR!
#10 3.227 npm ERR! While resolving: mongoose-currency@0.2.0
#10 3.227 npm ERR! Found: mongoose@7.0.3
#10 3.228 npm ERR! node_modules/mongoose
#10 3.229 npm ERR! mongoose@"^7.0.3" from the root project
#10 3.229 npm ERR!
#10 3.230 npm ERR! Could not resolve dependency:
#10 3.231 npm ERR! peer mongoose@"~> 4.x" from mongoose-currency@0.2.0
#10 3.231 npm ERR! node_modules/mongoose-currency
#10 3.232 npm ERR! mongoose-currency@"^0.2.0" from the root project
#10 3.233 npm ERR!
#10 3.233 npm ERR! Conflicting peer dependency: mongoose@4.13.21
#10 3.233 npm ERR! node_modules/mongoose
#10 3.234 npm ERR! peer mongoose@"~> 4.x" from mongoose-currency@0.2.0
#10 3.234 npm ERR! node_modules/mongoose-currency
#10 3.235 npm ERR! mongoose-currency@"^0.2.0" from the root project
#10 3.236 npm ERR!
#10 3.237 npm ERR! Fix the upstream dependency conflict, or retry
#10 3.238 npm ERR! this command with --force or --legacy-peer-deps
#10 3.238 npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
#10 3.238 npm ERR!
#10 3.238 npm ERR!
#10 3.238 npm ERR! For a full report see:
#10 3.238 npm ERR! /root/.npm/_logs/2023-04-28T09_06_28_271Z-eresolve-report.txt
#10 3.242
#10 3.242 npm ERR! A complete log of this run can be found in:
#10 3.242 npm ERR! /root/.npm/_logs/2023-04-28T09_06_28_271Z-debug-0.log
------
Error: failed to fetch an image or build from source: error building: executor failed running [/bin/sh -c
npm install --production=false]: exit code: 1
It seems that it has nothing to do with mongoose, but I'm not sure what is actually happening. This is my Dockerfile:
# syntax = docker/dockerfile:1
# Adjust NODE_VERSION as desired
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
# Install node modules
COPY --link package.json package-lock.json .
RUN npm install --production=false
# Copy application code
COPY --link . .
# 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 this my 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.1.2",
"mongoose": "^7.0.3",
"mongoose-currency": "^0.2.0",
"morgan": "^1.10.0"
},
"devDependencies": {
"nodemon": "^2.0.22"
}
}
Okay, after 24 hours of debugging I finally solve the problem.
The main issue is that mongoose-currency requires a mongoose verion 4, and that's why the dependencies conflict happens. When I tried to run npm install
locally in my terminal it fails due to the dependencies conflict, but when I run the command npm install --legacy-peer-deps
locally in my terminal it works fine. The issue here was that the Docker file generated when I run the command flyctl launch
doesn't support a --legacy-peer-deps
option, and has been taken care off by one of fly.io employees who opened and issue on GitHub here https://github.com/fly-apps/dockerfile-node/issues/6.
To circumvent this issue I updated the Dockerfile to be 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" ]
Also, the package.json didn't include a "start" script which caused another issue while deploying. To solve this issue, I updated the package.json to be the folloing:
{
"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"
}
}
Finally, the app is deployed and running as it should. I posted the solution here just incase anyone encounters the same problem or a similar problem in the future.