【问题标题】:Typescript compiler can't find node with yarn - Cannot find type definition file for 'node'打字稿编译器找不到带有纱线的节点-找不到“节点”的类型定义文件
【发布时间】:2021-10-27 05:56:32
【问题描述】:

我已经安装了@types/node。我正在使用纱线 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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      似乎问题在于 tsc (Typescript v4.3.5) 目前不支持 PnP,需要@yarnpkg/pnpify tool 作为解决方法。

      【讨论】:

        猜你喜欢
        • 2017-06-09
        • 2023-02-22
        • 2017-09-18
        • 2021-02-06
        • 1970-01-01
        • 2018-01-16
        • 2019-10-10
        • 2018-06-09
        • 1970-01-01
        相关资源
        最近更新 更多