构建我的 docker 容器时,GraphQL ERESOLVE 无法解析依赖树 [英] GraphQL ERESOLVE unable to resolve dependency tree when building my docker container

查看:53
本文介绍了构建我的 docker 容器时,GraphQL ERESOLVE 无法解析依赖树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的文件.

这里是我认为问题的核心.

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" ]

依赖错误

$ 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

推荐答案

这里的问题肯定出在 NPM 和您尝试安装的软件包上,而不是与 Docker 有任何关系.

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:

  • 从现在到发生此问题的这段时间发生了一些变化;
  • 您没有向我们展示一些重要的细节.

无论哪种方式,都有解决此类问题的通用方法,这应该会有所帮助.但首先要解释一下.

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

NPM 的包(依赖)管理机制允许包(依赖)拥有:

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

  • (direct) dependencies - 随包自动安装;
  • 对等依赖项 - 必须由包的使用者手动安装.
  • (direct) dependencies - installed automatically with the package;
  • peer dependencies - have to be manually installed by the consumer of the package.

但是,NPM不允许同一个包的多个版本共存.

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.

由于这两个原因,如果一个包要求依赖项 A 为 v1,而另一个包要求相同的依赖项 A 为 v2,则会发生冲突.

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,这是当前(截至 2020 年 11 月)node:current 图像使用的版本.

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

NPM7 带来的最大变化可能与对等依赖关系有关 - NPM 现在应该能够自动安装它们,如果可能.在此处阅读更多内容.

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.

如文档中所述,在无法解决冲突的情况下,NPM 现在应该抛出 错误 而不是警告,这就是您所看到的.

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.

另一方面,我使用您的设置和 NPM v7.0.8 只设法得到警告并且没有错误,我不知道为什么.但是,报告的问题基本相同,因此解决方案应该非常相似.

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.

我知道的唯一解决方案是手动解决冲突 - 开发人员需要调整他们的依赖关系以配合.

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

在您的具体情况下,问题似乎出在 graphql 包上.最新的graphql包是v15,也是最新的type-graphql包(v1)的对等依赖.

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).

但是,apollo-server-express 有一些依赖项,显然只支持 graphql 直到并包括 v14.

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

当您等待 apollo-server-express 完全支持 v15 时,您可以通过降级唯一需要 v15 的包来完全选择 graphql v14.因此,如果您将 npm install 更改为:

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

它应该可以工作...请注意,我们正在显式安装 graphql@14type-graphql@0(是的,版本为零).

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

也会给你一些不好的建议.在某些情况下,缺少对等依赖关系可能不是问题,尤其是在您从不使用相关功能的情况下.在您的情况下,问题可能更小,因为您确实具有依赖项,而不是所需的版本.完全有可能一个错误的版本就可以了.如果您感到幸运(或者如果您确定自己正在这样做)并且您真的希望继续使用 graphql v15,您可以:

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:

  • 抑制任何 NPM 输出以消除错误;
  • 降级到 NPM v6,它的工作方式完全不同(尽管它仍会警告您存在对等依赖问题).

谨慎行事

这篇关于构建我的 docker 容器时,GraphQL ERESOLVE 无法解析依赖树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆