Angular项目部署遇TS2403错误:crypto变量重复声明求助
解决Angular项目Drone构建TS2403类型冲突错误
问题描述
本地构建正常,但通过Drone执行部署时,构建步骤抛出TS2403类型冲突错误:
Error: node_modules/@types/node/ts4.8/crypto.d.ts:4477:13 - error TS2403: Subsequent variable declarations must have the same type. Variable 'crypto' must be of type 'Crypto', but here has type 'Crypto'. 4477 var crypto: webcrypto.Crypto; ~~~~~~ node_modules/typescript/lib/lib.dom.d.ts:18084:13 18084 declare var crypto: Crypto; ~~~~~~ 'crypto' was also declared here.
项目依赖如下:
"dependencies": { "@angular/animations": "^14.1.0", "@angular/cdk": "^14.2.2", "@angular/common": "^14.1.0", "@angular/compiler": "^14.1.0", "@angular/core": "^14.1.3", "@angular/forms": "^14.1.0", "@angular/localize": "^14.1.0", "@angular/platform-browser": "^14.1.3", "@angular/platform-browser-dynamic": "^14.1.0", "@angular/router": "^14.1.3", "@angular/service-worker": "^14.1.3", "@ng-bootstrap/ng-bootstrap": "^13.0.0", "@nrwl/angular": "15.7.2", "@popperjs/core": "^2.10.2", "angular-ng-stepper": "^2.0.0", "bootstrap": "^5.2.0", "bootstrap-icons": "^1.9.1", "express": "^4.18.1", "ngx-json-viewer": "^3.2.1", "ngx-progressbar": "^9.0.0", "ngx-spinner": "^14.0.0", "ngx-toastr": "^15.0.0", "path": "^0.12.7", "rxjs": "~7.5.0", "typescript": "~4.7.2", "zone.js": "~0.11.4" }, "devDependencies": { "@angular-devkit/build-angular": "^14.1.3", "@angular-devkit/core": "^14.2.6", "@angular-devkit/schematics": "^14.2.6", "@angular/cli": "^14.2.6", "@angular/compiler-cli": "^14.1.0", "@nrwl/nx-cloud": "15.0.0", "@nrwl/workspace": "15.7.2", "@schematics/angular": "^14.2.11", "@types/jasmine": "~4.0.0", "@types/node": "^20.11.5", "jasmine-core": "~4.2.0", "karma": "~6.4.0", "karma-chrome-launcher": "~3.1.0", "karma-coverage": "~2.2.0", "karma-jasmine": "~5.1.0", "karma-jasmine-html-reporter": "~2.0.0", "nx": "15.7.2", "prettier": "^2.8.4", "tslib": "^2.4.0", "typescript": "~4.7.2" }
已尝试安装@types/node@20.11.5但未解决问题,Drone构建配置:
commands: - npm install - npm run build-stage
解决方案
1. 匹配@types/node与TypeScript版本
项目使用的TypeScript为~4.7.2,而@types/node@20.x系列要求TypeScript 4.8及以上版本,版本不兼容导致类型冲突。建议降级到TS4.7兼容的@types/node版本:
npm install @types/node@~18.17.0 --save-dev
2. 调整TypeScript编译配置
- 临时快速解决:跳过库类型检查
在tsconfig.json的compilerOptions中添加:"skipLibCheck": true - 精确控制类型引入:避免同时加载冲突类型
在compilerOptions中指定仅引入所需类型库:
若需保留DOM类型,可在特定文件中通过"types": ["node"]/// <reference types="node" />声明Node环境,或通过typeRoots限定类型文件路径。
3. 确保Drone环境依赖一致性
本地与Drone环境可能存在依赖缓存差异,修改Drone配置清理缓存后重新安装:
commands: - rm -rf node_modules package-lock.json - npm install - npm run build-stage
4. 检查Nx构建配置
因项目使用@nrwl/angular,需确认nx.json或项目级tsconfig.json中的类型配置是否被Nx构建流程覆盖,确保编译选项正确传递到构建任务中。
内容的提问来源于stack exchange,提问作者Komsho
相关产品推荐
相关产品推荐

