我安装了@types/node。我正在使用 Yarn 2 (PnP)。好像 Typescript 找不到 @types/node 包?
使用构建脚本运行
yarn build
webpack
显示有关无法解析模块(路径、缓冲区等)的错误
运行
tsc
显示 TS2688: Cannot find type definition file for 'node'.
运行“ts-node”但是可以正常工作。
我的 tsconfig.json 如下:
{
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src",
"module": "commonjs",
"target": "es6",
"jsx": "react",
"allowJs": true,
"moduleResolution": "node",
"esModuleInterop": true,
"lib": [
"ES2020",
"DOM"
],
"types": ["node"]
},
"include": [
"src/**/*"
]
}
我的 webpack.config.js
module.exports = {
mode: "development",
devtool: "inline-source-map",
entry: "./src/server.ts",
output: {
filename: "bundle.js"
},
resolve: {
extensions: [".tsx", ".ts", ".js"]
},
module: {
rules: [
{
test: /\.tsx?$/,
use: [{
loader: 'ts-loader',
options: {
transpileOnly: true,
experimentalWatchApi: true,
onlyCompileBundledFiles: true,
},
}],
exclude: /node_modules/,
}
]
}
};
问题似乎是 tsc (Typescript v4.3.5) 目前不支持 PnP,并且需要 @yarnpkg/pnpify 工具 作为解决方法。
我在我的电脑上通过重新安装node.js解决了这个问题,因为yarn所依赖的程序。只需下载node.js并运行安装程序 - 这与yarn包无关,尽管我尝试了各种yarn命令,甚至清除缓存,就像项目之前编译和运行的难题一样。
如果您在 VSCode 中收到此消息,可能是因为您需要将 Yarn PnP 与 VSCode 集成:
- 安装由 Yarn 团队维护的 ZipFS 扩展。
- 运行以下命令,将生成一个
文件:.vscode/settings.json
yarn dlx @yarnpkg/sdks vscode
- 出于安全原因,VSCode 要求您显式激活自定义 TS 设置:
- 在 TypeScript 文件中按 ctrl+shift+p
- 选择“选择 TypeScript 版本”
- 选择“使用工作区版本”