Cannot find type definition file for 'node'. Entry point of type library 'node' specified in compilerOptions (Azure Pipelines)

huangapple go评论98阅读模式
英文:

Cannot find type definition file for 'node'. Entry point of type library 'node' specified in compilerOptions (Azure Pipelines)

问题

我们有一个Node应用程序,我们正在尝试通过Azure Pipelines部署它。在本地运行正常。
当流水线运行时,在tsc命令(构建步骤)运行之后,它立即在构建步骤上失败,并显示以下错误:

error TS2688: 无法找到类型定义文件 for 'node'。
文件在程序中是因为:
在compilerOptions中指定的类型库 'node' 的入口点

在我们的构建代理上,构建后的@types文件夹除了validator和另一个随机包之外,一无所有。

我已经在本地执行了以下命令:
npm i --save-dev @types/node

package-lock已更新,并包含@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" ], /* 指定像'./node_modules/@types'一样的多个文件夹。 */
"types": [ "node"]
}
}

package.json中的devDependencies:

"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天前是成功的。我的同事做了一些看似不相关的代码更改,导致它失败(dotenv的问题)并出现了这个错误。我们回退了所有更改,清除了代理,并进行了全新部署,但是现在经过6小时的调试后,我们仍然卡在这个问题上。所有这些都与之前正常工作的提交匹配。没有其他更改。

在线上的每个帖子都说要使用--save-dev安装类型,我们已经尝试了这个解决方案,但毫无进展。

我们尝试了--save-dev命令,它更新了package.json。我们将其与tsconfig.json中的compilerOptions数组更新配对使用。

在线上的每个帖子都标记为已完成,但我们仍然出现相同的错误消息。

英文:

We have a node app that we are trying to deploy through Azure Pipelines. It works fine locally.
When the pipeline runs, it fails on the build step with this error immediately after the tsc command (build step) runs:

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

When built, the @types folder in node_modules on our build agent is missing everything except for validator and another random package.

I already did this command locally:
npm i --save-dev @types/node

The package-lock is updated and contains @types/node like so:

"@types/node": {
  "version": "14.18.47",
  "resolved": "https://registry.npmjs.org/@types/node/-/node-14.18.47.tgz",
  "integrity": "sha512-OuJi8bIng4wYHHA3YpKauL58dZrPxro3d0tabPHyiNF8rKfGKuVfr83oFlPLmKri1cX+Z3cJP39GXmnqkP11Gw==",
  "dev": true
},

This is the 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"]
  }
}

devDependncies in package.json:

"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"
  }

The pipeline is just 2 Bash stages that run npm ci, followed by npm run build. Build agents are temporary and should not be caching or storing node modules anywhere.

I've tried installing @types/node multiple times in the pipeline, same error. Node modules just doesnt have the @types folder, or really much else.

This is everything inside of 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

The weird part, which makes this impossible to debug, is that this pipeline ran green 3 days ago. My coworder did some seemingly irrelevant code changes that broke it (dotenv stuff) and caused this error. We reverted everything, wiped the agent, and fresh deployed and now 6 hours of debugging later we are still stuck on this issue. All of this matches the commit that was working before. Nothing else changed.

Every single thread online says to install types with save-dev, we have exhausted that solution and arrived nowhere.

We tried the save-dev command, which updates package json. We coupled this with updating the compilerOptions array in tsconfig.json.

Every single thread online is marked as complete with this solution, but the same error message persists for us.

答案1

得分: 1

如果NODE_ENV设置为production,它会忽略devDependencies。

当他们建议将关键的**@types/node**依赖项添加到devDependencies时,很少有人谈论这一点,但我猜大多数人不会将他们的代码部署到生产环境。

对我们来说,解决方案是在流水线中将NODE_ENV设置为development,运行安装,然后在构建之前将其设置回production。

然后使用npm prune --production进行修剪。

英文:

Turns out, if NODE_ENV is set to production, it ignores devDependencies.

Nobody talks about this when they say to install the critical @types/node dependency to devDependencies, but I guess most people don't deploy their code to prod.

The solution for us is to set NODE_ENV to development in the pipeline, run the install, then set it back to production before the build.

Then prune with npm prune --production

huangapple
  • 本文由 发表于 2023年5月25日 04:21:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76327152.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定