Baeldung Pro – Ops – NPI EA (cat = Baeldung on Ops)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

1. Overview

Developers often encounter various issues when working with Docker Compose. One scenario that may raise issues is trying to run JavaScript tools like Vite in a containerized environment. In the case of Vite, Docker Compose may throw the error vite not found during the build process.

In this tutorial, we discuss some common causes of this error and how to resolve it.

2. Brief Introduction to Vite and Docker Compose

Vite is an optimized and fast modern build tool meant for front-end development. For instance, it’s used in JavaScript bundling and hot module replacement (HMR). Thus, it can be utilized in JavaScript frameworks such as React, Vue.js, and Svelte.

Meanwhile, we can use Docker Compose to containerize a web application that bundles the front end using Vite. In this scenario, Docker Compose ensures that all necessary services like a database are available for the app to run. However, we can encounter difficulties when Docker Compose fails to find or run the Vite command during the build process.

3. Common Causes of vite not found

Here, let’s look at some common causes for the vite not found error in Docker Compose.

3.1. Vite Is Not Installed in the Docker Image

The most common cause for vite not found is when Vite is not present in the Docker Container. Docker containers are isolated environments whereby each container can only access software installed within it. Therefore, if the Dockerfile doesn’t install Vite, the build process fails with the vite not found error.

To solve this, we can add Vite as a dependency in our Dockerfile. For example:

# Use node:16 as the base image
FROM node:16

# Define /app as the working directory in the container
WORKDIR /app

# Copy package.json and similarly named files like package-lock.json to the working directory
COPY package*.json ./

# Install dependencies, including Vite
RUN npm install

# Copy the other application files
COPY . .

# Expose the port that Vite will use (default 3000)
EXPOSE 3000

# Run Vite when the container starts
CMD ["npm", "run", "dev"]

In the example above, the npm install command ensures that all dependencies in package.json are installed, including Vite. By default, npm install installs dependencies listed in package.json.

3.2. Vite Is Not Listed in package.json

If Vite is not in the package.json file as a dependency then running npm install will not install it. This can lead to the error in question during the Docker build process since Vite is not available. That’s why we need to ensure that it exists in the dependencies or devDependencies section in package.json.

To resolve the vite not found error, we can run a command to install Vite:

$ npm install vite --save-dev

After the installation, we can confirm that it now appears in the package.json file:

{
  ...
  "devDependencies": {
    "vite": "^6.0.5"
  }
}

Now, Docker Compose installs Vite when it executes npm install since it now exists in the package.json file.

3.3. Vite Is Not Installed Globally

Normally, we can install Vite both locally or globally for a project. However, we may encounter vite not found if we choose to install it locally (in the project’s node_modules directory) and then attempt to use the Vite command globally. To clarify, Docker Compose can’t locate the executable unless we install it globally or include the local directory node_modules/.bin in the PATH environment variable.

To install Vite globally we can modify the Dockerfile and add:

RUN npm install -g vite

Although installing Vite globally makes the vite command accessible out of a project, it can raise other issues for production or containerized environments. For example, it can give rise to version mismatches in a situation where different projects need different versions of Vite. Meanwhile, local installation installs Vite as a project dependency. It ensures consistency across environments by tying the specific version of Vite to the project.

Let’s now look at adding the local directory node_modules/.bin in the PATH as an alternative to this approach.

3.4. Issue With the Environment Configuration

Sometimes, the issue may be with the environment configuration in the Docker container rather than the installation of Vite in particular. This is because the system fails to locate it since the PATH environment variable doesn’t hold the directory where Vite is installed.

To handle this, we can modify the Dockerfile to add the directory containing Vite to the PATH environment variable:

ENV PATH /app/node_modules/.bin:$PATH

The line above instructs the system to look in the correct location for binaries installed locally such as Vite.

3.5. Setting an Incorrect Working Directory

Another common cause of the error vite not found is setting an incorrect working directory in the Dockerfile. In this situation, the build process fails to locate the Vite command because it’s searching in the wrong location. For this reason, we need to ensure that the directive WORKDIR points to the correct directory holding the project files including the package.json file. For instance, if package.json exists in the root directory of our project, we can add:

WORKDIR /app

This line sets the working directory to /app in the container.

3.6. Issues With the Volume and Build Context

In this scenario, Docker Compose doesn’t use the correct build context or volumes. For example, the Docker Compose configuration may mount a volume to the container that overwrites our application’s node_modules directory or other crucial files. As a consequence, the volume mount replaces the expected directory structure.

As a solution, we can correctly configure the docker-compose.yml file to ensure there is no overwriting of node_modules or other crucial files during the build process:

version: '3'
services:
  frontend:
    build: ./
    volumes:
      - .:/app
      - /app/node_modules
    ports:
      - "3000:3000"

In this configuration, we mount the specific volume /app/node_modules for the node_modules directory to avoid overwriting it. This prevents the volume from clearing the necessary dependencies. In detail, mounting the directory node_modules as a volume in docker-compose.yml ensures the dependencies are preserved inside the container while allowing code changes to reflect immediately. Otherwise, the mounted application directory may overwrite the node_modules directory inside the container causing the vite not found error or other dependency issues.

For build context, an incorrect build context can occur if the docker-compose.yml file points to an incorrect directory as the context. This can lead to missing files such as package.json during the build process. To demonstrate, let’s assume that our project exists in the frontend subdirectory. However, in the docker-compose.yml file example above, we set the build context to the root directory (./). Thus, the container doesn’t include the necessary files and therefore we need to ensure the build context matches the project’s location. Notably, we can use docker-compose config to debug configuration issues.

4. Conclusion

In this article, we discussed some common reasons why Docker Compose throws the vite not found error during the build process.

During the Docker Compose build process, this error appears when Vite is not accessible in the Docker container or not installed. By ensuring Vite is properly installed in the Docker image, the necessary environments are set and the working directory is correct we can resolve this issue and get the Vite application running in a containerized environment.

Thus, we can avoid issues such as vite not found and ensure a smooth development experience with Vite by understanding how to configure Docker Compose.