📌  相关文章
📜  docker sh: react-scripts: not found - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:30:32.043000             🧑  作者: Mango

Docker: sh: react-scripts: not found - Shell-Bash

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.

Possible Solutions
Solution 1: Install Dependencies

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" ]
Solution 2: Use Node Image with Dependencies

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" ]
Solution 3: Use 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" ]
Conclusion

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.