[英]Private github repo as dependency is extraneous on npm install
[英]Docker `npm install` with GitHub private repo dependency in TypeScript
我們有一個 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
之后npm install
,在它的 repo 被檢出后。 “准備”腳本是npm run build
,“構建”腳本是tsc -p . && npm run validate
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)。 回購被檢出並且安裝顯然成功,然后“准備”腳本(因此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 中。我們不知道如何解釋) .
我們嘗試的主要故障排除步驟包括:
npm install
[工作正常]。index.d.ts
文件(但這些包甚至不在依賴項目中): https : //github.com/microsoft/TypeScript/問題/27956似乎在相關的Docker層中調用的shell的用戶上下文中一定有什么東西導致TS使用了錯誤的package.json(即錯誤的依賴項),因為我們在Dockerfile中所做的非常簡單,它適用於除 Docker 層之外的任何地方。
[更新/回答我自己的問題] 我無法完全解釋這種行為,因為它沒有任何意義。 相反,我嘗試使用 ubuntu:20.04 而不是 node:16-alpine 圖像。 我不得不添加 node 和一堆依賴項,但完成后我能夠很好地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
希望對其他人有所幫助,因為我花了大約一周的“空閑”時間才達到這一點!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.