使用SSR+pnpm+preserveSymlinks构建Angular时出现TypeScript编译错误
Angular 17 SSR 添加后构建失败问题
我有一个原本构建正常的Angular应用,包含以下特殊配置:
- 在
angular.json中设置了preserveSymlinks: true - 通过
package.json依赖中的link:协议安装了一个本地开发库
执行ng add @angular/ssr添加SSR功能后,ng build命令执行失败,出现多个TypeScript编译错误,部分错误示例如下:
✘ [ERROR] TS2688: Cannot find type definition file for 'express-serve-static-core'. [plugin angular-compiler] node_modules/@types/express/index.d.ts:8:22: 8 │ /// <reference types="express-serve-static-core" /> ╵ ~~~~~~~~~~~~~~~~~~~~~~~~~ // ... ✘ [ERROR] TS2307: Cannot find module 'body-parser' or its corresponding type declarations. [plugin angular-compiler] node_modules/@types/express/index.d.ts:11:28: 11 │ import * as bodyParser from "body-parser"; ╵ ~~~~~~~~~~~~~ // ... ✘ [ERROR] TS7006: Parameter 'req' implicitly has an 'any' type. [plugin angular-compiler] server.ts:28:19: 28 │ server.get('*', (req, res, next) => { ╵ ~~~ // ... ✘ [ERROR] TS2339: Property 'listen' does not exist on type 'Express'. [plugin angular-compiler] server.ts:51:9: 51 │ server.listen(port, () => { ╵ ~~~~~~ // ... ✘ [ERROR] Could not resolve "debug" node_modules/express/lib/view.js:15:20: 15 │ var debug = require('debug')('express:view'); ╵ ~~~~~~~ You can mark the path "debug" as external to exclude it from the bundle, which will remove this error and leave the unresolved path in the bundle. You can also surround this "require" call with a try/catch block to handle this failure at run-time instead of bundle-time.
使用的软件版本:
- Angular/Angular CLI/Angular SSR:
17.3.2(当前最新版本) - 包管理器:pnpm
8.15.6(当前最新版本) - TypeScript:
5.4.3(与Angular兼容的最新版本)
内容的提问来源于stack exchange,提问作者davidlj95
相关产品推荐
相关产品推荐

