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

运行docker-compose up时Node.js/npm未安装,Django+Tailwind部署故障

问题:Docker化集成Tailwind的Django应用时Node.js/NPM找不到,且Tailwind热更新失效

背景

正在将集成Tailwind的Django应用Docker化,准备部署到Lightsail。参考了两篇教程的代码,单独运行均正常,但结合后出现两个问题:一是启动容器时提示找不到Node.js/NPM;二是Tailwind热更新偶尔生效后失效,普通HTML修改正常,Tailwind相关修改不生效。

错误信息

执行docker-compose up时出现如下错误:

CommandError:
It looks like node.js and/or npm is not installed or cannot be found.
Visit https://nodejs.org to download and install node.js for your system.

gmo-django-lightsail-tailwind-1 |

If you have npm installed and still getting this error message, set NPM_BIN_PATH variable in settings.py to match path of NPM executable in your system.
Example:
NPM_BIN_PATH = "/usr/local/bin/npm

当前配置

settings.py 相关配置

NPM_BIN_PATH = r"C:\Program Files\nodejs\npm.cmd"

Dockerfile

FROM python:3.11.1-alpine3.17

ENV PYTHONUNBUFFERED 1

COPY ./requirements.txt /requirements.txt

RUN apk add --update --no-cache \
  postgresql-client \
  build-base \
  postgresql-dev \
  musl-dev \
  zlib \
  zlib-dev \
  linux-headers

WORKDIR /gmo

RUN apk add --no-cache \
  musl-dev \
  curl \
  nodejs

RUN python -m venv /py && \
  /py/bin/pip install --upgrade pip && \
  /py/bin/pip install -r /requirements.txt

COPY ./scripts /scripts

RUN chmod -R +x /scripts

ENV PATH="/scripts:/py/bin:$PATH"

COPY ./gmo /gmo

WORKDIR /gmo

EXPOSE 80

CMD ["/scripts/run.sh"]

docker-compose.yml

version: "3.8"

services:
  gmo:
    build:
      context: .
    volumes:
      - ./gmo:/gmo
    ports:
      - 8000:8000
    command: sh -c "python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
    environment:
      DEBUG: 1
      DB_HOST: database
      DB_NAME: django-dev-db
      DB_USER: devuser
      DB_PASS: devpassword123
    depends_on:
      database:
        condition: service_healthy

  tailwind:
    build:
      context: .
    volumes:
      - ./gmo:/gmo
    command: python manage.py tailwind start
    restart: unless-stopped
    tty: true

  database:
    image: postgres:12-alpine
    volumes:
      - dev-db-data:/var/lib/postgresql/data
    environment:
      POSTGRES_DB: django-dev-db
      POSTGRES_USER: devuser
      POSTGRES_PASSWORD: devpassword123
    healthcheck:
      test: ["CMD", "pg_isready", "-q", "-d", "django-dev-db", "-U", "devuser"]
      interval: 5s
      timeout: 5s
      retries: 10

volumes:
  dev-db-data:

run.sh

#!/bin/sh
set -e

SECRET_KEY=nothing python manage.py tailwind install --no-input
SECRET_KEY=nothing python manage.py tailwind build --no-input
SECRET_KEY=nothing python manage.py collectstatic --no-input
python manage.py migrate
gunicorn -b :80 --chdir /gmo gmo.wsgi:application

解决方案

1. 修复Node.js/NPM找不到的问题

  • 问题根源:settings.py中配置的NPM_BIN_PATH是Windows本地路径,与容器的Alpine Linux环境不匹配;且Dockerfile仅安装了nodejs,Alpine的nodejs包不包含npm。
  • 修复步骤:
    1. 修改settings.py中的路径为容器内的NPM路径:
      NPM_BIN_PATH = "/usr/bin/npm"
      
    2. 更新Dockerfile,添加npm安装:
      RUN apk add --no-cache \
        musl-dev \
        curl \
        nodejs \
        npm
      

2. 修复Tailwind热更新失效问题

  • 问题根源:容器内文件监听机制限制、静态文件缓存、开发环境与生产环境脚本逻辑冲突。
  • 修复步骤:
    1. 确保开发环境静态文件配置正确:
      在settings.py中添加:
      STATIC_URL = '/static/'
      STATICFILES_DIRS = [BASE_DIR / 'static']
      if DEBUG:
          STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'
      
    2. 让Tailwind服务强制监听文件变化:
      修改docker-compose的tailwind服务配置,添加轮询环境变量:
      tailwind:
        build:
          context: .
        volumes:
          - ./gmo:/gmo
        command: python manage.py tailwind start
        restart: unless-stopped
        tty: true
        environment:
          - CHOKIDAR_USEPOLLING=true
      
    3. 分离开发/生产环境脚本逻辑:
      修改run.sh,仅在生产环境执行静态文件编译:
      #!/bin/sh
      set -e
      
      python manage.py migrate
      
      if [ "$DEBUG" = "0" ]; then
        SECRET_KEY=nothing python manage.py tailwind install --no-input
        SECRET_KEY=nothing python manage.py tailwind build --no-input
        SECRET_KEY=nothing python manage.py collectstatic --no-input
        gunicorn -b :80 --chdir /gmo gmo.wsgi:application
      else
        python manage.py runserver 0.0.0.0:8000
      fi
      
    4. 统一gmo服务启动命令:
      修改docker-compose的gmo服务配置,使用run.sh启动:
      gmo:
        build:
          context: .
        volumes:
          - ./gmo:/gmo
        ports:
          - 8000:8000
        command: /scripts/run.sh
        environment:
          DEBUG: 1
          DB_HOST: database
          DB_NAME: django-dev-db
          DB_USER: devuser
          DB_PASS: devpassword123
        depends_on:
          database:
            condition: service_healthy
      

3. 验证步骤

  1. 执行docker-compose down -v清理旧容器和数据卷
  2. 执行docker-compose build重新构建镜像
  3. 执行docker-compose up启动服务,检查是否还有Node.js/NPM错误
  4. 修改HTML中的Tailwind类,刷新页面验证热更新是否生效

内容的提问来源于stack exchange,提问作者Marcus100

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 18:54:59