Docker容器运行Cypress/included:8.6.0时遭遇dayjs依赖缺失错误的解决方案咨询
dayjs Dependency in Cypress Container First, let's clarify why this happens: the cypress/included image comes pre-installed with Cypress, but it doesn't include your project's custom dependencies like dayjs. When you mount your local project into the container, Cypress tries to resolve the modules referenced in your code, but can't find them because they aren't present in the container's filesystem.
1. Installing dayjs directly inside the container
If you want to fix this immediately with your current setup, follow these steps:
- First, access the running container's shell (if you used
-dto run it detached, usedocker exec -it <container-id> /bin/bashto connect) - Navigate to your project directory inside the container. Based on your run command, this should be
/root/UI_Test_Automation(adjust if your mount path differs) - If your project already has a
package.json, run:npm install dayjs --save-dev - If you don't have a
package.jsonyet, initialize npm first then install:npm init -y npm install dayjs --save-dev
After installing, you can run your Cypress tests as usual, and the dependency should be resolved.
2. Better long-term solutions to avoid missing dependencies
Installing dependencies manually inside the container works, but it's not persistent (if you recreate the container, you'll have to repeat the step). Here are more reliable approaches:
Option A: Mount your local node_modules (with caution)
If your local machine has the same Node.js version as the cypress/included:8.6.0 image (check with docker run --rm cypress/included:8.6.0 node -v), you can mount your local node_modules into the container alongside your project:
sudo docker run -d -it -v $PWD:/UI_Test_Automation -v $PWD/node_modules:/UI_Test_Automation/node_modules -w /UI_Test_Automation cypress/included:8.6.0 /bin/bash
Note: This can cause issues if Node versions mismatch, since some dependencies are built for specific Node versions.
Option B: Use a Dockerfile to build a custom image
Create a Dockerfile in your project root that extends the Cypress image and installs your dependencies upfront:
FROM cypress/included:8.6.0 WORKDIR /UI_Test_Automation COPY package*.json ./ RUN npm install COPY . .
Then build the image:
docker build -t my-cypress-image .
And run it:
sudo docker run -d -it my-cypress-image /bin/bash
This way, all your project dependencies (including dayjs) are pre-installed in the image, so you won't run into missing module issues when starting containers.
Option C: Install dependencies on container start
Modify your run command to install dependencies before dropping into the shell:
sudo docker run -d -it -v $PWD:/UI_Test_Automation -w /UI_Test_Automation cypress/included:8.6.0 /bin/bash -c "npm install && /bin/bash"
This will automatically run npm install every time you start the container, ensuring dependencies are present.
Also, a quick note on your original run command: you set the working directory to ~/UI_Test_Automation, but you mounted your project to /UI_Test_Automation. It's better to align these (use -w /UI_Test_Automation) to avoid confusion with paths inside the container.
内容的提问来源于stack exchange,提问作者alexis19apl

