![](/img/trans.png)
[英]Typescript: Cannot find module XWZ or its corresponding type declarations in Docker Container
[英]ts-jest failing in Docker container only, passes locally. "cannot find module or corresponding type declarations"
當我嘗試在 Docker 容器內運行測試時出現以下錯誤:
//test/Client-mock.spec.ts:1:27 - error TS2307: Cannot find module '../src/Client' or its corresponding type declarations.
import {client} from '../src/Client'
我也一直在關注一些與此類似的網站,我覺得我已經很接近了,但一直缺少正確的配置:
Jest + Typescript + 絕對路徑 (baseUrl) 給出錯誤:找不到模塊
我的文件結構:
-src
-- Client.ts
-test
-- Client-mock.spec.ts
在本地,我的測試僅在 jest.config.js 中通過:
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
我的本地yarn test
最初也通過了 package.json:
"jest": {
"roots": [
"<rootDir>/test/"
],
"preset": "ts-jest"
在本地,我的測試也通過了 tsconfig.json 中的配置:
{
"compilerOptions": {
"target": "ES6",
"moduleResolution": "node",
"resolveJsonModule": true,
"types": ["@types/jest", "node", "@types/node"],
"esModuleInterop": true
}
}
我的 Dockerfile 中也有這個:
FROM node:16.3.0-alpine
COPY ./src ./src
COPY ./expectations ./expectations
COPY ./package.json ./package.json
COPY ./tsconfig.json ./tsconfig.json
COPY ./jest.config.js ./jest.config.js
COPY ./test ./test
RUN yarn install
# Run as post start script to load expectations
CMD ["yarn", "start", "test"]
如果我在我的容器中ls
,我會看到我的測試文件和配置文件,在我的 node_modules 文件夾中我會看到 ts-jest。
嘗試在您的 dockerfile 的FROM *
之后使用WORKDIR /app
,這將使/app
目錄成為所有其他命令的當前目錄(因此您的應用程序將保存在其中),然后從中執行
(所以當執行任何命令時,它也被用作基本目錄)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.