Github Actions出现TS2307模块找不到错误求助(本地正常)
本地构建正常,但GitHub Actions构建时出现TS2307模块找不到错误
本地执行yarn build完全正常,但在GitHub Actions中构建时,TypeScript抛出TS2307错误,提示找不到模块或对应的类型声明。尝试过切换路径别名、相对路径,调整GitHub Actions配置,问题仍未解决。
错误信息
Run yarn build yarn run v1.22.19 warning package.json: No license field $ tsc Error: src/application/Feed/Activity/AddActivity.handler.ts(7,37): error TS2307: Cannot find module '@domain/Feed/Activity/Dto/Activity.response.dto' or its corresponding type declarations. Error: src/domain/Feed/Activity/Activity.mapper.ts(2,37): error TS2307: Cannot find module './Dto/Activity.response.dto' or its corresponding type declarations. Error: src/infrastructure/Serverless/Controller/Feed/Activity/AddActivity.controller.ts(4,37): error TS2307: Cannot find module '@domain/Feed/Activity/Dto/Activity.response.dto' or its corresponding type declarations. Error: src/infrastructure/Serverless/GetStream/Feed/Activity/Mapper/Activity.mapper.ts(6,37): error TS2307: Cannot find module '@domain/Feed/Activity/Dto/Activity.response.dto' or its corresponding type declarations. error Command failed with exit code 2. Error: Process completed with exit code 2.
相关配置
GitHub Actions 配置
name: APP_NAME on: push: branches: [ master ] pull_request: branches: [ master ] workflow_dispatch: jobs: build_test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 with: persist-credentials: false - uses: actions/setup-node@v3 with: node-version: lts/* cache: yarn - name: install run: yarn --immutable - name: build run: yarn build - name: Install B4A Cli run: curl https://raw.githubusercontent.com/back4app/parse-cli/back4app/installer.sh | /bin/bash - name: Configure B4A Cli and set it default run: echo $ACCOUNT_KEY | b4a configure accountkey && b4a default $APP_NAME - name: Deploy run: b4a deploy
package.json 配置
{ "description": "", "main": "src/main.js", "scripts": { "lint": "eslint src/", "test": "jest", "build": "tsc", "dev:parse": "mongodb-runner start && parse-server --appId 'APP' --masterKey 'MASTER' --clientKey 'CLIENT' --databaseURI mongodb://localhost/test --cloud ./cloud/main.js", "debug:parse": "mongodb-runner start && parse-server --appId 'APP' --masterKey 'MASTER' --clientKey 'CLIENT' --databaseURI mongodb://localhost/test --cloud ./cloud/main.js --verbose", "dev:dash": "parse-dashboard --dev --appId APPLICATION_ID --masterKey MASTER_KEY --serverURL http://localhost:1337/parse --appName MY_APP" }, "devDependencies": { "@types/jest": "^29.2.3", "@types/node": "^18.11.9", "@types/node-fetch": "^2.6.2", "@types/parse": "^3.0.1", "@types/uuid": "^8.3.4", "@typescript-eslint/eslint-plugin": "^5.42.1", "@typescript-eslint/parser": "^5.42.1", "eslint": "^8.27.0", "jest": "^29.3.1", "ts-jest": "^29.0.3", "ts-node": "^10.9.1", "typescript": "^4.8.4" }, "dependencies": { "class-transformer": "^0.5.1", "class-validator": "^0.13.2", "getstream": "^8.1.0", "module-alias": "^2.2.2", "parse-server": "^5.3.3", "reflect-metadata": "^0.1.13", "tsyringe": "^4.7.0", "uuid": "^9.0.0" }, "_moduleAliases": { "@root": "cloud/", "@domain": "cloud/domain", "@application": "cloud/application", "@infrastructure": "cloud/infrastructure" } }
tsconfig.json 配置
{ "compilerOptions": { "lib": ["es6"], "experimentalDecorators": true, "emitDecoratorMetadata": true, "target": "ES2022", "module": "commonjs", "moduleResolution": "node", "rootDir": "src/", "baseUrl": ".", "paths": { "@root/*": ["./src/*"], "@domain/*": ["./src/domain/*"], "@application/*": ["./src/application/*"], "@infrastructure/*": ["./src/infrastructure/*"], }, "outDir": "cloud", "removeComments": true, "isolatedModules": true, "allowSyntheticDefaultImports": true, "esModuleInterop": false, "forceConsistentCasingInFileNames": true, "strict": true, "strictPropertyInitialization": false, "alwaysStrict": true, "skipLibCheck": true }, "include": ["src/"], "exclude": ["src/**/*.spec.ts", "cloud", "node_modules"] }
解决方案
1. 排查文件名大小写问题
GitHub Actions运行在Linux环境,文件名严格区分大小写。如果本地是Windows/macOS,文件名大小写不敏感,可能导致本地能找到文件,但CI环境找不到:
- 核对代码中引用的文件名(比如
Activity.response.dto)和实际文件的大小写完全一致,避免出现activity.response.dto.ts这类大小写不匹配的情况。 - 修正所有大小写不一致的引用和文件名。
2. 对齐tsconfig路径别名与实际文件结构
当前tsconfig.json的路径别名指向src/目录,需确保别名映射和实际文件结构完全匹配:
- 检查
@domain/Feed/Activity/Dto/Activity.response.dto对应的实际文件src/domain/Feed/Activity/Dto/Activity.response.dto.ts是否存在。 - 确认
tsconfig.json的paths配置没有拼写错误,比如@domain/*是否正确对应./src/domain/*。
3. 验证相对路径引用正确性
针对错误中的相对路径问题:src/domain/Feed/Activity/Activity.mapper.ts引用./Dto/Activity.response.dto,需确认:
- 当前目录下是否存在
Dto文件夹,且文件夹内有Activity.response.dto.ts文件。 - 避免路径中出现大小写不一致(比如实际是
dto小写,代码中写Dto大写)。
4. 确保GitHub Actions拉取完整代码
在actions/checkout步骤中添加fetch-depth: 0,确保拉取完整的仓库内容,避免部分文件未被检出:
- uses: actions/checkout@v3 with: persist-credentials: false fetch-depth: 0
5. 在CI中检查文件结构
在build步骤前添加查看文件结构的命令,直观验证CI环境中是否存在目标文件:
- name: Verify file existence run: | ls -la src/domain/Feed/Activity/Dto/ ls -la src/domain/Feed/Activity/
6. 清理TS缓存后再构建
CI环境的缓存可能导致TS无法解析最新的文件结构,尝试清理缓存后再构建:
- name: build run: | rm -rf node_modules/.cache/typescript yarn build
内容的提问来源于stack exchange,提问作者Alberto Barboza
相关产品推荐
相关产品推荐

