dmitrich0
dmitrich0

Reputation: 7

Error: Cannot find module '/dmitrich0/frontend/src/app/npm run start'. DOCKER

I'm getting these errors when trying to docker-compose up:

Error: Cannot find module '/dmitrich0/frontend/src/app/npm run start' real-time-chat-nestjs-angular-frontend-1 | at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15) real-time-chat-nestjs-angular-frontend-1 | at Function.Module._load (internal/modules/cjs/loader.js:746:27) real-time-chat-nestjs-angular-frontend-1 | at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12) real-time-chat-nestjs-angular-frontend-1 | at internal/main/run_main_module.js:17:47 { real-time-chat-nestjs-angular-frontend-1 | code: 'MODULE_NOT_FOUND', real-time-chat-nestjs-angular-frontend-1 | requireStack: [] real-time-chat-nestjs-angular-frontend-1 | }

Error: Cannot find module '/dmitrich0/src/app/npm run start:dev' real-time-chat-nestjs-angular-api-1 | at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15) real-time-chat-nestjs-angular-api-1 | at Function.Module._load (internal/modules/cjs/loader.js:746:27) real-time-chat-nestjs-angular-api-1 | at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12) real-time-chat-nestjs-angular-api-1 | at internal/main/run_main_module.js:17:47 { real-time-chat-nestjs-angular-api-1 | code: 'MODULE_NOT_FOUND', real-time-chat-nestjs-angular-api-1 | requireStack: [] real-time-chat-nestjs-angular-api-1 | }

API Dockerfile:

############################
#########DEVELOPMENT########
############################
FROM node:14 AS development
WORKDIR /dmitrich0/src/app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
EXPOSE 3000

############################
#########PRODUCTION#########
############################
FROM node:14 AS production
ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}
WORKDIR /dmitrich0/src/app
COPY --from=development /dmitrich0/src/app .
EXPOSE 3000
CMD ["node", "dist/main"]

Front dockerfile:

############################
#########DEVELOPMENT########
############################
FROM node:14 AS development
WORKDIR /dmitrich0/frontend/src/app
COPY package*.json ./
RUN npm install
RUN npm install -g @angular/cli
COPY . .
RUN npm run build
EXPOSE 4200

############################
#########PRODUCTION#########
############################
FROM node:14 AS production
ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}
WORKDIR /dmitrich0/src/app
COPY --from=development /dmitrich0/src/app .
EXPOSE 8080
CMD ["node", "dist/main"]

docker-compose.yml

version: "3.8"

services:
  api:
    volumes:
      - ./api:/dmitrich0/src/app
      - /dmitrich0/src/app/node_modules
    build:
      dockerfile: Dockerfile
      context: ./api
      target: development
    command:
      - npm run start:dev
    depends_on:
      - postgres
    environment:
      DATABASE_URL: postgres://user:password@postgres:5432/db
      NODE_ENV: development
      PORT: 3000
    ports:
      - "3000:3000"
      - "9229:9229"
  frontend:
    build:
      dockerfile: Dockerfile
      context: ./frontend
      target: development
    command:
      -  npm run start
    volumes:
      - ./frontend:/dmitrich0/frontend/src/app
      - /dmitrich0/frontend/src/app/node_modules
    ports:
      - "4200:4200"
    links:
      - api

First time working with Docker. What can I do with this errors?

Tried to change paths in volumes, but I don't understand why there are modules 'npm run start:dev' and 'npm run start'.

It's just that the paths are written incorrectly somewhere, or I made a mistake that it's hard for me to guess now, because I haven't worked with Docker before.

Upvotes: 0

Views: 123

Answers (2)

SoyonL
SoyonL

Reputation: 1

I had similar issue in my multi-stage docker build. My first stage only installs dependencies(node_modules), and the second stage deploys the node app.

I copied node_module specifically in the next stage rather than copying everything, then it solved my problem.

Can you try this in the production stage of API Dockerfile?
COPY --from=development /{your WORKDIR}/node_modules ./node_modules

Upvotes: 0

David Maze
David Maze

Reputation: 159081

In the same way that there are two syntaxes for a Dockerfile RUN directive, a Compose command: can either be a string or a list of words. If it's a string, Compose uses a simplistic splitting algorithm and doesn't automatically run a shell; if it's a list then Compose uses that list of words directly without doing any further processing.

You're using a YAML list

command:
  - npm run start
# ^   on a second line, with a list marker

and so this is processed as a single word, much as if you ran 'npm run start' from an interactive shell.

The easy fix is to change this from a list to a string

command: npm run start

The specific error you're getting is because the node image has some complex logic in its entrypoint wrapper to let you docker run node index.js without repeating the node interpreter command. If you pass it something that's an actual command docker run node npm ... then it runs it, but if the first word of the command isn't a command, it's run via node. So you're getting the equivalent of node 'npm run start' which produces those errors.

(Another approach that's worth considering, especially for a front-end application, is starting the back-end in a container docker-compose up -d api and then using Node directly on the host system for routine development. This would let you delete the unnecessary multi-stage build and the subtle volumes: setup.)

Upvotes: 0

Related Questions