Vite not found on CI Deploy from GitHub

Hi.
I made a CI configuration from GitHub to fly.io and when the deploy are running stop with error “vite not fouund”

the fly.toml:


# fly.toml app configuration file generated for mike-portfolio on 2023-05-23T16:51:52-03:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = "mike-portfolio"
primary_region = "scl"

[env]
  PORT = "8080"

[http_service]
  internal_port = 3000
  force_https = true
  auto_stop_machines = true
  auto_start_machines = true
  min_machines_running = 0

The Docker File:

# 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 . .

# 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" ]

my package.json:

{
  "name": "react-training",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "start":"vite --port 8080",
    "dev": "vite --port 8088",
    "build": "vite build",
    "preview": "vite preview --port 8080"
  },
  "dependencies": {
    "axios": "^1.2.5",
    "bootstrap": "^5.2.3",
    "react": "^18.2.0",
    "react-anchor-link-smooth-scroll": "^1.0.12",
    "react-bootstrap": "^2.7.1",
    "react-dom": "^18.2.0",
    "react-router-dom": "^6.8.1",
    "swiper": "^8.4.6"
  },
  "devDependencies": {
    "@types/react": "^18.0.26",
    "@types/react-dom": "^18.0.9",
    "@vitejs/plugin-react": "^3.0.0",
    "autoprefixer": "^10.4.13",
    "postcss": "^8.4.21",
    "tailwindcss": "^3.2.4",
    "vite": "^4.3.8"
  }
}

Any body can help me?

I think your issue is defining ENV NODE_ENV=production before your RUN npm install --production=false. If I recall correctly, the environment variable is an override.

Putting that under the install should help.

1 Like

i was editing and remove “–production=false” and failed again. Here the log:


#12 0.476 > mike-portfolio@0.0.0 build
#12 0.476 > vite build --port 8088
#12 0.476 
#12 0.483 sh: 1: vite: not found
#12 ERROR: executor failed running [/bin/sh -c npm run build]: exit code: 127
------
 > [build 5/6] RUN npm run build:
#12 0.476 
#12 0.476 > mike-portfolio@0.0.0 build
#12 0.476 > vite build --port 8088
#12 0.476 
#12 0.483 sh: 1: vite: not found
------
Error: failed to fetch an image or build from source: error building: executor failed running [/bin/sh -c npm run build]: exit code: 127

Error: Process completed with exit code 1.

You also need to remove the ENV (or move it after the install command)

1 Like

tks pb30. Move after install works.

I’ve opened Better support for vite · Issue #23 · fly-apps/dockerfile-node · GitHub

Essentially what you have now working is running vite in development mode on your server, which does things like watch for file changes and hot reload those changes. This is great for development, but does not apply in production.

What vite build does is build static files that can be deployed using a web server like nginx. Detecting this and automatically doing the right thing would have eliminated your need to change the Dockerfile and run your vite project as intended.

1 Like

Yeah I do:

RUN npm install && NODE_ENV=production npm run build

Then copy assets from dist/ to be served by static webserver. Using npm as the CMD is not meant for production.

I probably said it poorly, we seem to be agreeing.

This Dockerfile does not run a static web server, and should.

1 Like

The dockers file that worked:

FROM node:17-alpine as build
WORKDIR /app
COPY package.json .
RUN npm i
COPY . .
RUN npm run build
FROM nginx
COPY ./nginx/nginx.conf /etc/nginx/nginx.conf
COPY --from=build /app/dist /usr/share/nginx/html
3 Likes

Yes!

Normally we assume that Node applications are deploying node as a web server. This is not the case for Vite applications, so the Dockerfile we produce is less helpful that it could be. If we detect a vite application, we should produce a Dockerfile that looks more like the one you posted.

2 Likes

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