简体   繁体   中英

Node.js error in docker

I using node:latest image. And get ModuleBuildError: Module build failed: ModuleBuildError: Module build failed: Error: spawn /hobover_web_client/node_modules/pngquant-bin/vendor/pngquant ENOENT .
Dockerfile

FROM node:latest

# set working directory
RUN mkdir -p /hobover_web_client
WORKDIR /hobover_web_client
ENV NPM_CONFIG_LOGLEVEL=warn
COPY package.json yarn.lock /hobover_web_client/

# install app dependencies   
RUN  rm -rf  node_modules/ && yarn install --ignore-scripts && yarn global add babel babel-cli webpack nodemon pngquant optipng recjpeg
ADD . /hobover_web_client

In docker-compose.yml

version: '2'
  hobover_web_client:
    container_name: hobover_web_client
    build: ./hobover_web_client
    command: yarn start    
    ports:
      - "8080:8080"
    volumes:
      - ./hobover_web_client:/hobover_web_client
      - /hobover_web_client/node_modules

Build work successfully, but up cause an error. How can I fix it if without docker it works?

Your issue the is mount of app and node_modules in the same directory. When you use below in docker-compose

  - ./hobover_web_client:/hobover_web_client

You are overshadowing the existing node_modules . So you need to use NODE_PATH to relocate your packages. Change your Dockerfile to below

FROM node:latest

# set working directory
RUN mkdir -p /hobover_web_client /node_modules
WORKDIR /hobover_web_client
ENV NPM_CONFIG_LOGLEVEL=warn NODE_PATH=/node_moudles
COPY package.json yarn.lock /hobover_web_client/

# install app dependencies   
RUN  yarn install --ignore-scripts && yarn global add babel babel-cli webpack nodemon pngquant optipng recjpeg
ADD . /hobover_web_client

Change your compose to below

version: '2'
  hobover_web_client:
    container_name: hobover_web_client
    build: ./hobover_web_client
    command: yarn start    
    ports:
      - "8080:8080"
    volumes:
      - ./hobover_web_client:/hobover_web_client
      - /node_modules

So now your /node_modules goes to a anonymous volume, which you as such don't need and can remove, because the path is inside different folder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM