You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Node.js项目Docker部署启动失败:无法找到指定模块

Node.js App Fails to Load in Docker: Cannot Find Module './src/Presentation/viewModel/index'

Let's break down why your Node.js app is throwing that module-not-found error in Docker, and walk through actionable fixes to resolve it.

First, the core issue is clear: your app.js file is trying to require ./src/Presentation/viewModel/index, but Docker's Linux-based environment (Alpine uses a case-sensitive filesystem) can't locate this file. Here are the most likely causes and solutions:

1. Case Sensitivity Mismatch

This is the most common culprit when moving from Windows/macOS (case-insensitive filesystems) to Linux.

  • Double-check your project's actual file and directory names: Is the file named Index.js (capitalized "I") instead of index.js? Or is the directory spelled presentation instead of Presentation?
  • Fix: Either update the require statement in app.js to exactly match the case of your file/directory names, or rename the files to align with the reference.

2. File Not Copied to the Container

Even though you explicitly copied the src directory, the file might not be making it into the Docker container. Let's verify this:
Modify your Dockerfile to add a quick directory check before starting the app:

FROM node:10-alpine
WORKDIR /home/node/app
COPY app.js /home/node/app
COPY bin /home/node/app/bin
COPY config /home/node/app/config
COPY public /home/node/app/public
COPY scratch /home/node/app/scratch
COPY src /home/node/app/src
COPY package.json /home/node/app
COPY package-lock.json /home/node/app
RUN npm install
# Add this line to list files in the viewModel directory
RUN ls -la /home/node/app/src/Presentation/viewModel/
EXPOSE 3000
CMD npm start

Rebuild and run the container—this will print exactly what files exist in that directory. If index.js is missing:

  • Check if the file is ignored in .gitignore (common for generated files like compiled JS from TypeScript)
  • Swap individual COPY commands for COPY . . (paired with a proper .dockerignore file) to copy all project files, which reduces the chance of missing critical assets.

3. Missing Build Step (For Transpiled Code)

If your project uses TypeScript or another language that requires transpilation to JavaScript, you might be copying uncompiled source files to Docker, but your require statement is looking for the compiled JS output.
Fix: Add a build step to your Dockerfile:

FROM node:10-alpine
WORKDIR /home/node/app
# Copy package files first to leverage Docker cache
COPY package.json package-lock.json ./
RUN npm install
# Copy all project files
COPY . .
# Run your project's build command (adjust to match your package.json script)
RUN npm run build
EXPOSE 3000
CMD npm start

4. Relative Path Misalignment

Double-check the relative path in your require statement. Since app.js lives in /home/node/app, ./src/Presentation/viewModel/index should resolve to /home/node/app/src/Presentation/viewModel/index.js—this is correct, but if your local project structure differs from what's in Docker, this could break. The ls check from step 2 will confirm if the path matches.

Bonus: Clean Up Your Build with .dockerignore

Create a .dockerignore file in your project root to exclude unnecessary files from being copied to Docker:

node_modules/
.git/
dist/
.env

This keeps your Docker image small and avoids overwriting critical files like node_modules with your local version.

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

火山引擎 最新活动