Docker 在根目录下安装 yarn 导致 tsc rootDir 错误

Docker installing yarn at root causing tsc rootDir error

我有一个节点项目,它是一个简单的服务器问候世界。 运行 在本地,这一切都可以正常编译并完美运行。然而,当在 Docker 中时,会创建一个 /opt/yarn-v1.21.1 文件夹,这会导致 rootDir 错误:

error TS6059: File '/opt/yarn-v1.21.1/bin/yarn.js' is not under 'rootDir' '/opt/src'. 'rootDir' is expected to contain all source files.

Docker文件

FROM node:13.8.0-alpine

WORKDIR /opt

COPY package*.json ./
COPY tsconfig*.json ./

RUN npm i --quiet

tsconfig

{
    "compilerOptions": {
        "target": "es5",                          
        "module": "commonjs",                    
        "lib": ["es6"],                     
        "allowJs": true,
        "outDir": "dist",                          
        "rootDir": "src",
        "strict": true,         
        "noImplicitAny": true,
        "esModuleInterop": true,
        "resolveJsonModule": true,
    },
}

package.json

{
    "name": "test-api",
    "scripts": {
        "dev": "tsc",
        "start:dev": "tsc && concurrently \"tsc -w\" \"nodemon dist/app.js\""
    },
    "dependencies": {
        "@types/node": "^13.7.0",
        "concurrently": "^5.1.0",
        "nodemon": "^2.0.2",
        "typescript": "^3.7.5"
    }
}

任何见解将不胜感激,为什么 运行 这到 docker 会有所不同。我跳到的唯一假设是 yarn 已经安装在我的系统上,但默认情况下它安装在图像中。

值得一提的是,将 rootDir 从 /src 更改为 ./ 确实解决了错误,但光盘文件夹中的输出变为:

dist
  src
    app.js
  yarn-v1.21.1

而不是:

dist
  app.js

将 Dockerfile 中的 WORKDIR 命令更新为以下内容:

WORKDIR /app

基础镜像已经在 /opt 目录中安装了 yarn。当您将您的应用程序也添加到 /opt 文件夹中时,它会认为文件夹 yarn-1.21.1 也是您的应用程序的一部分,因此会出现错误。也许你也可以使用 WORKDIR /opt/app 如果你真的想要你的应用程序在 /opt.