📅  最后修改于: 2023-12-03 15:30:32.043000             🧑  作者: Mango
If you're encountering the error message sh: react-scripts: not found
when running a React application in a Docker container, you're not alone. This error typically indicates that the necessary dependencies are not available in the Docker container. In this guide, we'll explore possible solutions to this error.
One possible solution is to install the necessary dependencies in the Docker container. This can be achieved by modifying the Dockerfile
to include the necessary npm
or yarn
commands to install the dependencies, or by manually installing them inside the container. Here's an example Dockerfile
that installs the necessary dependencies using npm
:
FROM node:14-alpine
# Set working directory
WORKDIR /usr/src/app
# Copy project files to working directory
COPY . .
# Install app dependencies
RUN npm install
# Build app
RUN npm run build
# Start app
CMD [ "npm", "start" ]
Another possible solution is to use a Node image that already has the necessary dependencies installed. This can help avoid the need to manually install dependencies in the container. Here's an example Dockerfile
that uses an image with the necessary dependencies:
FROM node:14-alpine
# Set working directory
WORKDIR /usr/src/app
# Copy project files to working directory
COPY . .
# Build app
RUN npm run build
# Start app
CMD [ "npm", "start" ]
npx
If you're using create-react-app
to generate your React application, you can use npx
to run the react-scripts
command instead of installing it globally. Here's an example Dockerfile
that uses npx
:
FROM node:14-alpine
# Set working directory
WORKDIR /usr/src/app
# Copy project files to working directory
COPY . .
# Build app
RUN npx react-scripts build
# Start app
CMD [ "npm", "start" ]
In this guide, we explored possible solutions to the error message sh: react-scripts: not found
when running a React application in a Docker container. By installing the necessary dependencies or using an image that already has them, you can avoid this error and run your React application successfully in a Docker container.