【问题标题】:docker container build failingdocker容器构建失败
【发布时间】:2021-10-05 16:58:53
【问题描述】:

我正在尝试将我的服务移动到 Docker 容器中。在本地构建项目可以正常工作,但是当我尝试将其构建为 docker build 的一部分时,出现以下错误:

node_modules/jest-extended/types/index.d.ts(135,39): error TS2694: Namespace 'jest' has no exported member 'Mock'.

请找到我的 dockerfile:

FROM node:12
COPY package.json /opt/service/
WORKDIR /opt/service/
RUN npm install
COPY . .
RUN npm run build
...

相关的package.json部分

"scripts": {
  "build": "rm -rf lib && graphql-codegen && tsc -p tsconfig.json && copyfiles -u 1 ./src/**/*.graphql lib && copyfiles -u 1 ./src/**/*.proto lib"
},
"devDependencies": {
  "@graphql-codegen/cli": "1.17.8",
  "@graphql-codegen/typescript": "1.17.8",
  "@graphql-codegen/typescript-resolvers": "^1.18.1",
  "@types/jest": "^27.0.2",
  "@types/node": "^16.10.2",
  "copyfiles": "^2.3.0",
  "graphql": "14.7.0",
  "jest": "27.2.4",
  "jest-cli": "27.2.4",
  "jest-extended": "^0.11.5",
  "nodemon": "^2.0.4",
  "ts-jest": "^27.0.5",
  "ts-node": "^10.2.1",
  "typescript": "^3.9.7"
},

tsconfig.json

"compilerOptions": {
  "target": "es2016",
  "module": "commonjs",
  "lib": ["es2019", "dom"],
  "strict": true,
  "noImplicitAny": true,
  "noUnusedLocals": true,
  "noUnusedParameters": true,
  "noImplicitReturns": true,
  "typeRoots": ["node_modules/@types"],
  "esModuleInterop": true,
  "resolveJsonModule": true,
  "outDir": "lib",
  "rootDir": "src",
  "resolveJsonModule": true,
  "allowJs": true
},
"ts-node": {
  "transpileOnly": true,
  "files": true
},
"include": ["**/*"],
"exclude": ["node_modules", "lib", "jest.*.ts", "jest.*.js", "**/generated/*"]

编辑:

在我的 compilerOptions 中添加 "skipLibCheck": true 修复了 Namespace 'jest' has no exported member 'Mock' error.

虽然我之前没有提到,但我仍然遇到错误,我希望通过修复第一个错误来解决。

我仍然从 *.test.ts 文件中得到的错误是:

error TS2708: Cannot use namespace 'jest' as a value.
error TS2304: Cannot find name 'expect'.
error TS2304: Cannot find name 'beforeAll'.
error TS2582: Cannot find name 'test'.

【问题讨论】:

  • compilerOptions 中添加"skipLibCheck": true 会有影响吗?
  • @StefanGolubović 确实有所作为。命名空间错误现在消失了。我仍然收到我之前没有提到的错误,“不能使用命名空间'jest'作为值”和“找不到名称'beforeAll'”以及与beforeAll相同的错误,用于期望、测试和描述。如果我设法修复命名空间错误,我希望它们会自动解决:) 你是否也知道如何修复这些错误?
  • Typescript 不是我的强项。也许您可以尝试将types 添加到compilerOptions。我的猜测是 tsconfig.json 出了点问题,这在本地设置中无法重现(例如,类型信息缓存在某处或类似的地方)。

标签: node.js docker jestjs


【解决方案1】:

我有一个想法,也许可以解决您的问题,因为我有一个类似的问题是由于版本冲突引起的。您能否尝试将您的 package-lock.json 文件也复制到您的 docker 映像中?

...
COPY package.json /opt/service/
COPY package-lock.json /opt/service/
...

【讨论】:

  • 感谢您的建议。我确实尝试过这样做,但仍然遇到相同的错误。当您运行 npm install 时,会生成一个新的 package-lock.json。但是,如果您使用npm ci,则使用 package-lock.json,但它需要与 package.json 同步。我尝试复制这两个文件并运行npm ci但是,就像我之前说的,仍然遇到同样的错误。
【解决方案2】:

这原来是 tsconfig.json 和我的文件夹结构的问题。我在这个项目中有多个服务和包。在根文件夹(每个服务的两个文件夹)中,我有一个 tsconfig.json 文件,其他服务和包正在扩展该文件。因为我只是从根目录而不是 node_modules 复制 tsconfig.json 文件,并且根 tsconfig.json 文件声明了"typeRoots": ["node_modules/@types"],所以服务期望在../../node_modules 中找到@types。

【讨论】:

    猜你喜欢
    • 2021-12-02
    • 1970-01-01
    • 2023-02-08
    • 1970-01-01
    • 1970-01-01
    • 2019-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多