首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GitHub私有回购依赖于TypeScript中的Docker‘`npm’

GitHub私有回购依赖于TypeScript中的Docker‘`npm’
EN

Stack Overflow用户
提问于 2021-10-17 02:26:01
回答 1查看 661关注 0票数 0

我们有一个Node TypeScript项目,我们正在尝试文档化。该项目依赖于另一个GitHub私有回购,它通过package.json中的“git@github.com:{ private - repo }”语法引用。依赖项目也是TS项目。当在任何本地dev操作系统(如npm install、Ubuntu等)的克隆位置和外壳中运行npm ci (或npm ci等)时,主项目都会安装并构建良好。然而,当试图对主项目进行文档化时,我们看到的显然是没有意义的npm build脚本错误。依赖项目有一个“准备”脚本,该脚本在对依赖项目调用的npm install之后运行,该脚本的repo已经签出。“准备”脚本是npm run build,“构建”脚本是tsc -p . && npm run validate

所以事情是这样的:

主要项目的package.json:

代码语言:javascript
复制
{
    "name": "main-project",
    "private": true,
    "scripts": {
        ...
    },
    "dependencies": {
        ...
        "typescript": "^4.3.4",
        "private-repo": "git@github.com:my-private-repo.git#a-branch",
    },
    "devDependencies": {
        "@types/example": "^1.0.0",
        ...
    }
}

依赖项目package.json:

代码语言:javascript
复制
{
    "name": "dependency-project",
    "main": "dist/index.js",
    "types": "dist/index.d.ts",
    "scripts": {
        "build": "tsc -p . && npm run validate",
        "prepare": "npm run build",
        "validate": "node dist/validate.js"
    },
    "private": true,
    "dependencies": {
        ...
    },
    "devDependencies": {
        "@types/example": "1.0.0",
        ...
    }
}

总体目标是在层中构建Docker映像,但是我们在第一步(主要项目的npm install)完成的第一步就遇到了困难。

主项目的Dockerfile如下所示:

代码语言:javascript
复制
FROM node:16-alpine
ARG SSH_KEY
RUN apk add git openssh-client
COPY package.json package-lock.json ./
RUN mkdir -p -m 0600 ~/.ssh && ssh-keyscan github.com >> ~/.ssh/known_hosts
RUN ssh-agent sh -c 'echo $SSH_KEY | base64 -d | ssh-add - ; npm ci'

这种将私钥交给层构建的方法工作得很好(尽管它是各种方法(包括Docker )中唯一能够工作的方法)。回购得到签出,安装显然成功,然后“准备”脚本(因此npm buildtsc -p)运行。

当我们运行docker build --build-arg SSH_KEY=$key .时,一切都运行良好,直到出现以下错误:

代码语言:javascript
复制
#9 27.31 npm ERR! > my-private-repo@0.0.3 prepare
#9 27.31 npm ERR! > npm run build
#9 27.31 npm ERR!
#9 27.31 npm ERR!
#9 27.31 npm ERR! > my-private-repo@0.0.3 build
#9 27.31 npm ERR! > tsc -p . && npm run validate
#9 27.31 npm ERR!
#9 27.31 npm ERR! error TS2688: Cannot find type definition file for 'cacheable-request'.
#9 27.31 npm ERR!   The file is in the program because:
#9 27.31 npm ERR!     Entry point for implicit type library 'cacheable-request'
#9 27.31 npm ERR! error TS2688: Cannot find type definition file for 'chai'.
#9 27.31 npm ERR!   The file is in the program because:
#9 27.31 npm ERR!     Entry point for implicit type library 'chai'
#9 27.31 npm ERR! error TS2688: Cannot find type definition file for 'cors'.
#9 27.31 npm ERR!   The file is in the program because:
#9 27.31 npm ERR!     Entry point for implicit type library 'cors'
#9 27.31 npm ERR! error TS2688: Cannot find type definition file for 'faker'.
#9 27.31 npm ERR!   The file is in the program because:
#9 27.31 npm ERR!     Entry point for implicit type library 'faker'
#9 27.31 npm ERR! error TS2688: Cannot find type definition file for 'lodash'.
#9 27.31 npm ERR!   The file is in the program because:
#9 27.31 npm ERR!     Entry point for implicit type library 'lodash'
#9 27.31 npm ERR! error TS2688: Cannot find type definition file for 'mocha'.
#9 27.31 npm ERR!   The file is in the program because:
#9 27.31 npm ERR!     Entry point for implicit type library 'mocha'
#9 27.31 npm ERR! error TS2688: Cannot find type definition file for 'responselike'.
#9 27.31 npm ERR!   The file is in the program because:
#9 27.31 npm ERR!     Entry point for implicit type library 'responselike'

令人困惑的是,那些“错误TS2688”消息所引用的包都不是依赖项(私有回购)项目(它们位于项目的package-lock.json中)。我们不知道该怎么解释。

我们尝试过的主要故障排除步骤包括:

在本地dev PC操作系统上使用相同的Node和npm版本并运行主项目的fine.

  • Trying

似乎在相关的Docker层中调用的shell的用户上下文中一定有什么东西会导致TS使用错误的package.json (即错误的依赖项),因为我们在Dockerfile中所做的事情非常简单,而且除了在Docker层之外,它在任何地方都能工作。

EN

回答 1

Stack Overflow用户

发布于 2021-10-23 05:21:08

更新/回答我自己的问题--我无法完全解释这种行为,因为它没有任何意义。相反,我尝试使用ubuntu:20.04而不是节点:16-高寒图像。我必须添加节点和一组依赖项,但是这样做之后,我就可以很好地npm ci了,没有类型记录的抱怨,也没有任何其他的抱怨。

下面是Dockerfile,以防万一对任何人都有帮助(注意这只是基本/构建层):

代码语言:javascript
复制
FROM ubuntu:20.04

RUN apt-get update \
  # had to install tzdata this first to get the noninteractive to work
  && DEBIAN_FRONTEND="noninteractive" apt-get install -y --no-install-recommends tzdata \
  && apt-get install -y curl gnupg build-essential libcurl4-openssl-dev openssh-client git\
  && curl -fsSL https://deb.nodesource.com/setup_16.x | bash - \
  #&& apt-get remove -y --purge cmdtest \
  && apt-get update \
  && apt-get install -y nodejs \
  && rm -rf /var/lib/apt/lists/* \
  && rm -rf /var/lib/apt/lists.d/* \
  && apt-get autoremove \
  && apt-get clean \
  && apt-get autoclean

RUN adduser --disabled-password --gecos "" --uid 1000 node

RUN chown -R node:node /home/node

USER node

# had to set mode to 0700 otherwise couldn't open .ssh director to write known_hosts file
RUN mkdir -p -m 0700 ~/.ssh && ssh-keyscan github.com >> ~/.ssh/known_hosts
RUN mkdir -p ~/app/node_modules && chown -R node:node /home/node/app

WORKDIR ~/app

COPY package.json package-lock.json ./

# had to allow uid=1000 access for this to work
RUN --mount=type=ssh,uid=1000 npm ci

COPY --chown=node:node . .

RUN npm run build

希望这对其他人有帮助,因为我花了大约一个星期的“空闲”时间才达到这一点!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69600885

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档