【发布时间】:2021-12-04 14:42:01
【问题描述】:
我们有一个 Node TypeScript 项目,我们正在尝试 Dockerize。该项目依赖于另一个 GitHub 私有仓库,该仓库通过 package.json 中的“git@github.com:{private-repo-name}”语法引用。依赖项目也是一个TS项目。在任何本地开发 PC 操作系统(例如 macOS、Ubuntu LTS 等)的克隆位置和 shell 中运行 npm install(或 npm ci 等)时,主项目安装和构建良好。然而,当我们尝试 Dockerize 主项目时,我们看到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 Buildkit)。 repo 被签出并且安装显然成功,然后“准备”脚本(因此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”消息所指的包都不是依赖项(私有 repo)项目的依赖项(它们位于 main 的 package-lock.json 中项目。我们不知道如何解释。
我们尝试过的主要故障排除步骤包括:
- 在本地开发 PC 操作系统上使用相同的 Node 和 npm 版本,并运行主项目的
npm install[工作正常]。 - 在这篇文章之后,尝试在它所抱怨的每个包中查找
index.d.ts文件(但这些包甚至不在依赖项目中):https://github.com/microsoft/TypeScript/issues/27956 - 试图理解和检查这个帖子,无济于事:Fixing TS2688: Cannot find type definition file in node_modules
似乎在相关 Docker 层中调用的 shell 的用户上下文中一定有某些东西导致 TS 使用错误的 package.json(即错误的依赖项),因为我们在 Dockerfile 中所做的是非常简单,除了 Docker 层外,它在任何地方都可以使用。
【问题讨论】:
标签: node.js git docker npm typescript2.0