2

我安装了@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/,
      }
    ]
  }
};
4

2 回答 2

1

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

于 2021-08-27T10:42:19.053 回答
0

I solved it on my computer by re-installing node.js, as the program yarn is relying on. Simply download node.js and run the installer - it was nothing to do with yarn packages, though I tried all sorts of yarn commands, even clearing the cache, as a puzzle as the project had previously compiled and run.

于 2021-09-30T07:30:29.893 回答