我们有一个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:
{
"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:
{
"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如下所示:
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 build和tsc -p)运行。
当我们运行docker build --build-arg SSH_KEY=$key .时,一切都运行良好,直到出现以下错误:
#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.
index.d.ts文件(但这些包甚至不在依赖项中):https://github.com/microsoft/TypeScript/issues/27956
似乎在相关的Docker层中调用的shell的用户上下文中一定有什么东西会导致TS使用错误的package.json (即错误的依赖项),因为我们在Dockerfile中所做的事情非常简单,而且除了在Docker层之外,它在任何地方都能工作。
发布于 2021-10-23 05:21:08
更新/回答我自己的问题--我无法完全解释这种行为,因为它没有任何意义。相反,我尝试使用ubuntu:20.04而不是节点:16-高寒图像。我必须添加节点和一组依赖项,但是这样做之后,我就可以很好地npm ci了,没有类型记录的抱怨,也没有任何其他的抱怨。
下面是Dockerfile,以防万一对任何人都有帮助(注意这只是基本/构建层):
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希望这对其他人有帮助,因为我花了大约一个星期的“空闲”时间才达到这一点!
https://stackoverflow.com/questions/69600885
复制相似问题