GraphQL ERESOLVE unable to resolve dependency tree when building my docker container

这一生的挚爱 提交于 2020-12-30 08:14:31

问题


Here are my files.

Here is I think the core of the problem.

Could not resolve dependency:
npm ERR! peer graphql@"^0.12.0 || ^0.13.0 || ^14.0.0" from graphql-middleware@4.0.2

docker-compose.yml

version: '3.7'

services:
  apollo:
    container_name: apollo
    build:
      context: .
      dockerfile: Dockerfile
    environment:
        -  NODE_ENV=development
    volumes:
      - '.:/app'
      - '/app/node_modules'

    ports:
      - 4000:4000

    restart: always

Dockerfile

# Use the official image as a parent image.
FROM node:current-slim

# Set the working directory.
WORKDIR /app

# Setting environment path.
ENV PATH=/app/node_modules/.bin:$PATH

# Copy the file from your host to your current location.
COPY package.json .

# Run the command inside your image filesystem.
RUN npm init --yes
RUN npm install --save cors apollo-server-express express graphql reflect-metadata type-graphql apollo-datasource-rest soap jsonwebtoken --yes
RUN npm install nodemon -g --yes

# Add metadata to the image to describe which port the container is listening on at runtime.
EXPOSE 4000

# Copy the rest of your app's source code from your host to your image filesystem.
COPY . .
CMD [ "nodemon", "index.js" ]

Dependency Error

$ docker-compose up --build
Building apollo
Step 1/10 : FROM node:current-slim
 ---> f3f62dfcc735
Step 2/10 : WORKDIR /app
 ---> Using cache
 ---> 33088e65c748
Step 3/10 : ENV PATH=/app/node_modules/.bin:$PATH
 ---> Using cache
 ---> c7f742267b26
Step 4/10 : COPY package.json .
 ---> Using cache
 ---> 76285ea4a8ca
Step 5/10 : RUN npm init --yes
 ---> Using cache
 ---> 29a3d715136b
Step 6/10 : RUN npm install --save cors apollo-server-express express graphql reflect-metadata type-graphql apollo-datasource-rest soap jsonwebtoken --yes
 ---> Running in 1e4472bcd901
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: apollo-express-server@1.0.0
npm ERR! Found: graphql@15.4.0
npm ERR! node_modules/graphql
npm ERR!   graphql@"^15.3.0" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer graphql@"^0.12.0 || ^0.13.0 || ^14.0.0" from graphql-middleware@4.0.2
npm ERR! node_modules/graphql-middleware
npm ERR!   graphql-middleware@"^4.0.2" from the root project
npm ERR! 
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
npm ERR! 
npm ERR! See /root/.npm/eresolve-report.txt for a full report.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-11-05T16_19_42_605Z-debug.log
ERROR: Service 'apollo' failed to build : The command '/bin/sh -c npm install --save cors apollo-server-express express graphql reflect-metadata type-graphql apollo-datasource-rest soap jsonwebtoken --yes' returned a non-zero code: 1

回答1:


The problem here is certainly with NPM and the packages you are trying to install rather than anything to do with Docker.

Unfortunately, I am not able to reproduce the exact error that you are facing. That could be because:

  • something changed in the time between now and whenever this problem occurred;
  • there are some essential details that you are not showing us.

Either way, there's a general way in which such issues are solved, which should help. But first an explanation.

Dependencies, peer dependencies and conflicts

NPM's package (dependency) management mechanism allows packages (dependencies) to have:

  • (direct) dependencies - installed automatically with the package;
  • peer dependencies - have to be manually installed by the consumer of the package.

However, NPM does not allow multiple versions of the same package to coexist.

Also, as you may know, packages use standard semantic versioning, which means that a major version change indicates a breaking change.

Due to these two reasons, clashes occur if one package requires dependency A to be v1, while another wants the same dependency A to be v2.

NPM v7

NPM v7 was recently released and this is the version that current (as of November 2020) node:current images use.

Probably the biggest changes brought about by NPM7 relate to peer dependencies - NPM should now be able to install them automatically, if possible. Read more here.

As described in the document, in cases where it's not possible to solve the conflicts, NPM should now throw errors rather than warnings, which is what you are seeing.

I, on the other hand, only managed to get warnings and no errors using your setup and NPM v7.0.8, and I don't know why. The problems reported were essentially the same, however, so the resolution ought to be very similar.

How to solve conflicts

The only solution that I'm aware of is manual conflict resolution - the developer needs to adjust their dependencies to play along.

In your specific case the problem seems to be with the graphql package. The latest graphql package is v15, which is also a peer dependency of the latest type-graphql package (v1).

However, apollo-server-express has a few dependencies, which apparently only support graphql up to and including v14.

While you wait for apollo-server-express to fully support v15, you may opt for graphql v14 altogether by downgrading the only package that requires v15. So if you change your npm install to this:

npm install --save cors apollo-server-express express graphql@14 reflect-metadata type-graphql@0 apollo-datasource-rest soap jsonwebtoken

it ought to work... Notice that we are explicitly installing graphql@14 and type-graphql@0 (yes, version zero).

Alternative solution

Going to give you some bad advice too. In some cases a missing peer dependency may not be a problem, particularly if you never use the related functionality. In your case, it may be even less of a problem because you do have the dependency, just not the required version. It's entirely possible that a wrong version would do just fine. If you feel lucky (or if you're sure of you're doing) and you really wish to proceed with graphql v15, you could either:

  • suppress any NPM output to silence the errors;
  • downgrade to NPM v6, which works quite differently (although it will still warn you of peer dependency problems).

Proceed with caution!



来源:https://stackoverflow.com/questions/64701308/graphql-eresolve-unable-to-resolve-dependency-tree-when-building-my-docker-conta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!