我们有一个节点应用程序,正在尝试通过 Azure Pipelines 进行部署。在本地运行良好。 当管道运行时,它在 tsc 命令(构建步骤)运行后立即在构建步骤中失败并出现此错误:
error TS2688: Cannot find type definition file for 'node'.
The file is in the program because:
Entry point of type library 'node' specified in compilerOptions
构建时,构建代理上的 node_modules 中的 @types 文件夹缺少除验证器和另一个随机包之外的所有内容。
我已经在本地执行了这个命令:
npm i --save-dev @types/node
包锁已更新并包含@types/node,如下所示:
"@types/node": {
"version": "14.18.47",
"resolved": "https://registry.npmjs.org/@types/node/-/node-14.18.47.tgz",
"integrity": "sha512-OuJi8bIng4wYHHA3YpKauL58dZrPxro3d0tabPHyiNF8rKfGKuVfr83oFlPLmKri1cX+Z3cJP39GXmnqkP11Gw==",
"dev": true
},
这是 tsconfig.json:
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es6",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"typeRoots": [ "./node_modules/@types" ], /* Specify multiple folders that act like './node_modules/@types'. */
"types": [ "node"]
}
}
package.json 中的 devDependency:
"devDependencies": {
"@types/express": "^4.17.17",
"@types/node": "^14.18.47",
"@types/node-cron": "^3.0.7",
"ts-loader": "^8.0.17",
"ts-node": "^9.1.1",
"tsconfig-paths": "^3.9.0",
"typescript": "^4.1.5"
}
管道只是 2 个 Bash 阶段,运行 npm ci,然后运行 npm run build。构建代理是临时的,不应该在任何地方缓存或存储节点模块。
我尝试在管道中多次安装@types/node,同样的错误。节点模块只是没有 @types 文件夹,或者其他很多东西。
这是node_modules/@types 中的所有内容
ls -lah node_modules/@types
total 16K
drwxr-xr-x 4 vsts docker 4.0K May 24 18:59 .
drwxr-xr-x 132 vsts docker 4.0K May 24 18:59 ..
drwxr-xr-x 2 vsts docker 4.0K May 24 18:59 triple-beam
drwxr-xr-x 4 vsts docker 4.0K May 24 18:59 validator
奇怪的是,这条管道在 3 天前运行为绿色,这使得它无法调试。我的 coorder 做了一些看似不相关的代码更改,破坏了它(dotenv 的东西)并导致了这个错误。我们恢复了所有内容,擦除了代理,并进行了全新部署,现在经过 6 小时的调试,我们仍然陷入这个问题。所有这些都与之前正在运行的提交相匹配。其他都没有改变。
网上的每个线程都说要使用 save-dev 安装类型,我们已经用尽了该解决方案但一无所获。
我们尝试了 save-dev 命令,它更新了包 json。我们将其与更新 tsconfig.json 中的 compilerOptions 数组结合起来。
使用此解决方案,每个在线线程都被标记为完成,但相同的错误消息仍然存在。
事实证明,如果 NODE_ENV 设置为生产环境,它会忽略 devDependency。
当他们说要安装对 devDependency 的关键 @types/node 依赖时,没有人谈论这一点,但我猜大多数人不会将他们的代码部署到产品中。
我们的解决方案是将 NODE_ENV 设置为管道中的开发,运行安装,然后在构建之前将其设置回生产。
然后用
npm prune --production
修剪
对我来说这是一个不兼容的
typescript
版本。我一次提出了几个依赖项,并且 typescript
对于 angular
来说太高了。