Typescript 编译器无法找到带有纱线的节点 - 无法找到“节点”的类型定义文件

问题描述 投票:0回答:3

我安装了@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/,
      }
    ]
  }
};
typescript webpack yarnpkg yarnpkg-v2
3个回答
1
投票

问题似乎是 tsc (Typescript v4.3.5) 目前不支持 PnP,并且需要 @yarnpkg/pnpify 工具 作为解决方法。


0
投票

我在我的电脑上通过重新安装node.js解决了这个问题,因为yarn所依赖的程序。只需下载node.js并运行安装程序 - 这与yarn包无关,尽管我尝试了各种yarn命令,甚至清除缓存,就像项目之前编译和运行的难题一样。


0
投票

如果您在 VSCode 中收到此消息,可能是因为您需要将 Yarn PnP 与 VSCode 集成

  1. 安装由 Yarn 团队维护的 ZipFS 扩展。
  2. 运行以下命令,将生成一个
    .vscode/settings.json
    文件:
    yarn dlx @yarnpkg/sdks vscode
    
  3. 出于安全原因,VSCode 要求您显式激活自定义 TS 设置:
    1. 在 TypeScript 文件中按 ctrl+shift+p
    2. 选择“选择 TypeScript 版本”
    3. 选择“使用工作区版本”
© www.soinside.com 2019 - 2024. All rights reserved.