You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Strapi官方Docker容器二次启动时出现lodash模块找不到错误求助

Fix: "Cannot find module 'lodash'" on second docker-compose up for Strapi Docker

I’ve run into this exact issue before with Strapi’s official Docker setup—here are the most reliable fixes to get you back up and running:

Root Cause

This usually happens because the node_modules directory (automatically installed during the first container run) isn’t preserved correctly between restarts. If you’re mounting your local project folder directly into the container, it can overwrite or skip reinstalling dependencies when you launch the container a second time.


Step-by-Step Solutions

1. Reset Container Volumes (Quickest First Fix)

Start by wiping existing containers and their associated volumes to clear any corrupted dependency caches:

docker-compose down -v

Then restart with a fresh build to ensure all dependencies are reinstalled properly:

docker-compose up --build

2. Clean Local Files (If Mounting the Entire Project)

If your docker-compose.yml mounts your local directory to /usr/src/api/strapi-app, your local filesystem might be missing lodash or have a broken lock file. Run these commands in your local project folder first:

rm -rf node_modules package-lock.json

Then restart the containers again with docker-compose up --build.

3. Refine Your Docker Volume Mounts

Mounting the entire project directory often causes conflicts with container-managed dependencies. Instead, mount only the critical directories you need to edit locally:

# In your docker-compose.yml
services:
  api:
    image: strapi/strapi
    volumes:
      - ./config:/usr/src/api/strapi-app/config
      - ./src:/usr/src/api/strapi-app/src
      - ./package.json:/usr/src/api/strapi-app/package.json
      # Leave node_modules to be managed by the container or a dedicated volume

This way, the container’s node_modules stays intact between restarts.

4. Force Dependency Install on Startup

Add a command to your docker-compose.yml to ensure dependencies are checked and installed every time the container starts:

services:
  api:
    image: strapi/strapi
    command: sh -c "npm install && npm run start"
    # ... rest of your existing config

This adds a small startup delay but guarantees no missing modules on restart.


内容的提问来源于stack exchange,提问作者André Kirchner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:09:09